home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / bfd / elf64-alpha.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  93.5 KB  |  3,336 lines

  1. /* ALPHA-specific support for 64-bit ELF
  2.    Copyright 1996 Free Software Foundation, Inc.
  3.    Contributed by Richard Henderson <rth@tamu.edu>.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  20.  
  21. /* We need a published ABI spec for this.  Until one comes out, don't
  22.    assume this'll remain unchanged forever.  */
  23.  
  24. #include "bfd.h"
  25. #include "sysdep.h"
  26. #include "libbfd.h"
  27. #include "elf-bfd.h"
  28.  
  29. #include "elf/alpha.h"
  30.  
  31. #define ALPHAECOFF
  32.  
  33. #define NO_COFF_RELOCS
  34. #define NO_COFF_SYMBOLS
  35. #define NO_COFF_LINENOS
  36.  
  37. /* Get the ECOFF swapping routines.  Needed for the debug information. */
  38. #include "coff/internal.h"
  39. #include "coff/sym.h"
  40. #include "coff/symconst.h"
  41. #include "coff/ecoff.h"
  42. #include "coff/alpha.h"
  43. #include "aout/ar.h"
  44. #include "libcoff.h"
  45. #include "libecoff.h"
  46. #define ECOFF_64
  47. #include "ecoffswap.h"
  48.  
  49. static struct bfd_hash_entry * elf64_alpha_link_hash_newfunc
  50.   PARAMS((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
  51. static struct bfd_link_hash_table * elf64_alpha_bfd_link_hash_table_create
  52.   PARAMS((bfd *));
  53.  
  54. static bfd_reloc_status_type elf64_alpha_reloc_nil
  55.   PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
  56. static bfd_reloc_status_type elf64_alpha_do_reloc_gpdisp
  57.   PARAMS((bfd *, bfd_vma, bfd_byte *, bfd_byte *));
  58. static bfd_reloc_status_type elf64_alpha_reloc_gpdisp
  59.   PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
  60. static bfd_reloc_status_type elf64_alpha_reloc_op_push
  61.   PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
  62. static bfd_reloc_status_type elf64_alpha_reloc_op_store
  63.   PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
  64. static bfd_reloc_status_type elf64_alpha_reloc_op_psub
  65.   PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
  66. static bfd_reloc_status_type elf64_alpha_reloc_op_prshift
  67.   PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
  68.  
  69. static reloc_howto_type * elf64_alpha_bfd_reloc_type_lookup
  70.   PARAMS((bfd *, bfd_reloc_code_real_type));
  71. static void elf64_alpha_info_to_howto
  72.   PARAMS((bfd *, arelent *, Elf64_Internal_Rela *));
  73.  
  74. static boolean elf64_alpha_object_p
  75.   PARAMS((bfd *));
  76. static boolean elf64_alpha_section_from_shdr
  77.   PARAMS((bfd *, Elf64_Internal_Shdr *, char *));
  78. static boolean elf64_alpha_fake_sections
  79.   PARAMS((bfd *, Elf64_Internal_Shdr *, asection *));
  80. static int elf64_alpha_additional_program_headers
  81.   PARAMS((bfd *));
  82. static boolean elf64_alpha_create_got_section
  83.   PARAMS((bfd *, struct bfd_link_info *));
  84. static boolean elf64_alpha_create_dynamic_sections
  85.   PARAMS((bfd *, struct bfd_link_info *));
  86.  
  87. static boolean elf64_alpha_read_ecoff_info
  88.   PARAMS((bfd *, asection *, struct ecoff_debug_info *));
  89. static boolean elf64_alpha_is_local_label
  90.   PARAMS((bfd *, asymbol *));
  91. static boolean elf64_alpha_find_nearest_line
  92.   PARAMS((bfd *, asection *, asymbol **, bfd_vma, const char **,
  93.       const char **, unsigned int *));
  94.  
  95. #if defined(__STDC__) || defined(ALMOST_STDC)
  96. struct alpha_elf_link_hash_entry;
  97. #endif
  98.  
  99. static boolean elf64_alpha_output_extsym
  100.   PARAMS((struct alpha_elf_link_hash_entry *, PTR));
  101.  
  102. static boolean elf64_alpha_check_relocs
  103.   PARAMS((bfd *, struct bfd_link_info *, asection *sec,
  104.       const Elf_Internal_Rela *));
  105. static boolean elf64_alpha_adjust_dynamic_symbol
  106.   PARAMS((struct bfd_link_info *, struct elf_link_hash_entry *));
  107. static boolean elf64_alpha_size_dynamic_sections
  108.   PARAMS((bfd *, struct bfd_link_info *));
  109. static boolean elf64_alpha_adjust_dynindx
  110.   PARAMS((struct elf_link_hash_entry *, PTR));
  111. static boolean elf64_alpha_relocate_section
  112.   PARAMS((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  113.       Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
  114. static boolean elf64_alpha_finish_dynamic_symbol
  115.   PARAMS((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
  116.       Elf_Internal_Sym *));
  117. static boolean elf64_alpha_finish_dynamic_sections
  118.   PARAMS((bfd *, struct bfd_link_info *));
  119. static boolean elf64_alpha_final_link
  120.   PARAMS((bfd *, struct bfd_link_info *));
  121.  
  122.  
  123. #define alpha_elf_tdata(bfd) \
  124.     ((struct alpha_elf_obj_tdata *)elf_tdata(bfd)->tdata)
  125.  
  126. struct alpha_elf_link_hash_entry
  127. {
  128.   struct elf_link_hash_entry root;
  129.  
  130.   /* External symbol information.  */
  131.   EXTR esym;
  132.  
  133.   unsigned char flags;
  134.   /* Contexts (LITUSE) in which a literal was referenced.  */
  135. #define ALPHA_ELF_LINK_HASH_LU_ADDR 01
  136. #define ALPHA_ELF_LINK_HASH_LU_MEM 02
  137. #define ALPHA_ELF_LINK_HASH_LU_FUNC 04
  138. };
  139.  
  140. /* Alpha ELF linker hash table.  */
  141.  
  142. struct alpha_elf_link_hash_table
  143. {
  144.   struct elf_link_hash_table root;
  145. };
  146.  
  147. /* Look up an entry in a Alpha ELF linker hash table.  */
  148.  
  149. #define alpha_elf_link_hash_lookup(table, string, create, copy, follow)    \
  150.   ((struct alpha_elf_link_hash_entry *)                    \
  151.    elf_link_hash_lookup (&(table)->root, (string), (create),        \
  152.              (copy), (follow)))
  153.  
  154. /* Traverse a Alpha ELF linker hash table.  */
  155.  
  156. #define alpha_elf_link_hash_traverse(table, func, info)            \
  157.   (elf_link_hash_traverse                        \
  158.    (&(table)->root,                            \
  159.     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),    \
  160.     (info)))
  161.  
  162. /* Get the Alpha ELF linker hash table from a link_info structure.  */
  163.  
  164. #define alpha_elf_hash_table(p) \
  165.   ((struct alpha_elf_link_hash_table *) ((p)->hash))
  166.  
  167. /* Create an entry in a Alpha ELF linker hash table.  */
  168.  
  169. static struct bfd_hash_entry *
  170. elf64_alpha_link_hash_newfunc (entry, table, string)
  171.      struct bfd_hash_entry *entry;
  172.      struct bfd_hash_table *table;
  173.      const char *string;
  174. {
  175.   struct alpha_elf_link_hash_entry *ret =
  176.     (struct alpha_elf_link_hash_entry *) entry;
  177.  
  178.   /* Allocate the structure if it has not already been allocated by a
  179.      subclass.  */
  180.   if (ret == (struct alpha_elf_link_hash_entry *) NULL)
  181.     ret = ((struct alpha_elf_link_hash_entry *)
  182.        bfd_hash_allocate (table,
  183.                   sizeof (struct alpha_elf_link_hash_entry)));
  184.   if (ret == (struct alpha_elf_link_hash_entry *) NULL)
  185.     return (struct bfd_hash_entry *) ret;
  186.  
  187.   /* Call the allocation method of the superclass.  */
  188.   ret = ((struct alpha_elf_link_hash_entry *)
  189.      _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  190.                      table, string));
  191.   if (ret != (struct alpha_elf_link_hash_entry *) NULL)
  192.     {
  193.       /* Set local fields.  */
  194.       memset (&ret->esym, 0, sizeof (EXTR));
  195.       /* We use -2 as a marker to indicate that the information has
  196.      not been set.  -1 means there is no associated ifd.  */
  197.       ret->esym.ifd = -2;
  198.       ret->flags = 0;
  199.     }
  200.  
  201.   return (struct bfd_hash_entry *) ret;
  202. }
  203.  
  204. /* Create a Alpha ELF linker hash table.  */
  205.  
  206. static struct bfd_link_hash_table *
  207. elf64_alpha_bfd_link_hash_table_create (abfd)
  208.      bfd *abfd;
  209. {
  210.   struct alpha_elf_link_hash_table *ret;
  211.  
  212.   ret = ((struct alpha_elf_link_hash_table *)
  213.      bfd_zalloc (abfd, sizeof (struct alpha_elf_link_hash_table)));
  214.   if (ret == (struct alpha_elf_link_hash_table *) NULL)
  215.     return NULL;
  216.  
  217.   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
  218.                        elf64_alpha_link_hash_newfunc))
  219.     {
  220.       bfd_release (abfd, ret);
  221.       return NULL;
  222.     }
  223.  
  224.   return &ret->root.root;
  225. }
  226.  
  227.  
  228. /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
  229.    from smaller values.  Start with zero, widen, *then* decrement.  */
  230. #define MINUS_ONE    (((bfd_vma)0) - 1)
  231.  
  232. static reloc_howto_type elf64_alpha_howto_table[] =
  233. {
  234.   HOWTO (R_ALPHA_NONE,        /* type */
  235.      0,            /* rightshift */
  236.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  237.      8,            /* bitsize */
  238.      true,            /* pc_relative */
  239.      0,            /* bitpos */
  240.      complain_overflow_dont, /* complain_on_overflow */
  241.      elf64_alpha_reloc_nil,    /* special_function */
  242.      "NONE",        /* name */
  243.      false,            /* partial_inplace */
  244.      0,            /* src_mask */
  245.      0,            /* dst_mask */
  246.      true),            /* pcrel_offset */
  247.  
  248.   /* A 32 bit reference to a symbol.  */
  249.   HOWTO (R_ALPHA_REFLONG,    /* type */
  250.      0,            /* rightshift */
  251.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  252.      32,            /* bitsize */
  253.      false,            /* pc_relative */
  254.      0,            /* bitpos */
  255.      complain_overflow_bitfield, /* complain_on_overflow */
  256.      0,            /* special_function */
  257.      "REFLONG",        /* name */
  258.      false,            /* partial_inplace */
  259.      0xffffffff,        /* src_mask */
  260.      0xffffffff,        /* dst_mask */
  261.      false),        /* pcrel_offset */
  262.  
  263.   /* A 64 bit reference to a symbol.  */
  264.   HOWTO (R_ALPHA_REFQUAD,    /* type */
  265.      0,            /* rightshift */
  266.      4,            /* size (0 = byte, 1 = short, 2 = long) */
  267.      64,            /* bitsize */
  268.      false,            /* pc_relative */
  269.      0,            /* bitpos */
  270.      complain_overflow_bitfield, /* complain_on_overflow */
  271.      0,            /* special_function */
  272.      "REFQUAD",        /* name */
  273.      false,            /* partial_inplace */
  274.      MINUS_ONE,        /* src_mask */
  275.      MINUS_ONE,        /* dst_mask */
  276.      false),        /* pcrel_offset */
  277.  
  278.   /* A 32 bit GP relative offset.  This is just like REFLONG except
  279.      that when the value is used the value of the gp register will be
  280.      added in.  */
  281.   HOWTO (R_ALPHA_GPREL32,    /* type */
  282.      0,            /* rightshift */
  283.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  284.      32,            /* bitsize */
  285.      false,            /* pc_relative */
  286.      0,            /* bitpos */
  287.      complain_overflow_bitfield, /* complain_on_overflow */
  288.      0,            /* special_function */
  289.      "GPREL32",        /* name */
  290.      false,            /* partial_inplace */
  291.      0xffffffff,        /* src_mask */
  292.      0xffffffff,        /* dst_mask */
  293.      false),        /* pcrel_offset */
  294.  
  295.   /* Used for an instruction that refers to memory off the GP register.  */
  296.   HOWTO (R_ALPHA_LITERAL,    /* type */
  297.      0,            /* rightshift */
  298.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  299.      16,            /* bitsize */
  300.      false,            /* pc_relative */
  301.      0,            /* bitpos */
  302.      complain_overflow_signed, /* complain_on_overflow */
  303.      0,            /* special_function */
  304.      "LITERAL",        /* name */
  305.      false,            /* partial_inplace */
  306.      0xffff,        /* src_mask */
  307.      0xffff,        /* dst_mask */
  308.      false),        /* pcrel_offset */
  309.  
  310.   /* This reloc only appears immediately following a LITERAL reloc.
  311.      It identifies a use of the literal.  The symbol index is special:
  312.      1 means the literal address is in the base register of a memory
  313.      format instruction; 2 means the literal address is in the byte
  314.      offset register of a byte-manipulation instruction; 3 means the
  315.      literal address is in the target register of a jsr instruction.
  316.      This does not actually do any relocation.  */
  317.   HOWTO (R_ALPHA_LITUSE,    /* type */
  318.      0,            /* rightshift */
  319.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  320.      32,            /* bitsize */
  321.      false,            /* pc_relative */
  322.      0,            /* bitpos */
  323.      complain_overflow_dont, /* complain_on_overflow */
  324.      elf64_alpha_reloc_nil,    /* special_function */
  325.      "LITUSE",        /* name */
  326.      false,            /* partial_inplace */
  327.      0,            /* src_mask */
  328.      0,            /* dst_mask */
  329.      false),        /* pcrel_offset */
  330.  
  331.   /* Load the gp register.  This is always used for a ldah instruction
  332.      which loads the upper 16 bits of the gp register.  The symbol
  333.      index of the GPDISP instruction is an offset in bytes to the lda
  334.      instruction that loads the lower 16 bits.  The value to use for
  335.      the relocation is the difference between the GP value and the
  336.      current location; the load will always be done against a register
  337.      holding the current address.
  338.  
  339.      NOTE: Unlike ECOFF, partial inplace relocation is not done.  If
  340.      any offset is present in the instructions, it is an offset from
  341.      the register to the ldah instruction.  This lets us avoid any
  342.      stupid hackery like inventing a gp value to do partial relocation
  343.      against.  Also unlike ECOFF, we do the whole relocation off of
  344.      the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair.  An odd,
  345.      space consuming bit, that, since all the information was present
  346.      in the GPDISP_HI16 reloc.  */
  347.   HOWTO (R_ALPHA_GPDISP,    /* type */
  348.      16,            /* rightshift */
  349.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  350.      16,            /* bitsize */
  351.      false,            /* pc_relative */
  352.      0,            /* bitpos */
  353.      complain_overflow_dont, /* complain_on_overflow */
  354.      elf64_alpha_reloc_gpdisp, /* special_function */
  355.      "GPDISP",        /* name */
  356.      false,            /* partial_inplace */
  357.      0xffff,        /* src_mask */
  358.      0xffff,        /* dst_mask */
  359.      true),            /* pcrel_offset */
  360.  
  361.   /* A 21 bit branch.  */
  362.   HOWTO (R_ALPHA_BRADDR,    /* type */
  363.      2,            /* rightshift */
  364.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  365.      21,            /* bitsize */
  366.      true,            /* pc_relative */
  367.      0,            /* bitpos */
  368.      complain_overflow_signed, /* complain_on_overflow */
  369.      0,            /* special_function */
  370.      "BRADDR",        /* name */
  371.      false,            /* partial_inplace */
  372.      0x1fffff,        /* src_mask */
  373.      0x1fffff,        /* dst_mask */
  374.      true),            /* pcrel_offset */
  375.  
  376.   /* A hint for a jump to a register.  */
  377.   HOWTO (R_ALPHA_HINT,        /* type */
  378.      2,            /* rightshift */
  379.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  380.      14,            /* bitsize */
  381.      true,            /* pc_relative */
  382.      0,            /* bitpos */
  383.      complain_overflow_dont, /* complain_on_overflow */
  384.      0,            /* special_function */
  385.      "HINT",        /* name */
  386.      false,            /* partial_inplace */
  387.      0x3fff,        /* src_mask */
  388.      0x3fff,        /* dst_mask */
  389.      true),            /* pcrel_offset */
  390.  
  391.   /* 16 bit PC relative offset.  */
  392.   HOWTO (R_ALPHA_SREL16,    /* type */
  393.      0,            /* rightshift */
  394.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  395.      16,            /* bitsize */
  396.      true,            /* pc_relative */
  397.      0,            /* bitpos */
  398.      complain_overflow_signed, /* complain_on_overflow */
  399.      0,            /* special_function */
  400.      "SREL16",        /* name */
  401.      false,            /* partial_inplace */
  402.      0xffff,        /* src_mask */
  403.      0xffff,        /* dst_mask */
  404.      false),        /* pcrel_offset */
  405.  
  406.   /* 32 bit PC relative offset.  */
  407.   HOWTO (R_ALPHA_SREL32,    /* type */
  408.      0,            /* rightshift */
  409.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  410.      32,            /* bitsize */
  411.      true,            /* pc_relative */
  412.      0,            /* bitpos */
  413.      complain_overflow_signed, /* complain_on_overflow */
  414.      0,            /* special_function */
  415.      "SREL32",        /* name */
  416.      false,            /* partial_inplace */
  417.      0xffffffff,        /* src_mask */
  418.      0xffffffff,        /* dst_mask */
  419.      false),        /* pcrel_offset */
  420.  
  421.   /* A 64 bit PC relative offset.  */
  422.   HOWTO (R_ALPHA_SREL64,    /* type */
  423.      0,            /* rightshift */
  424.      4,            /* size (0 = byte, 1 = short, 2 = long) */
  425.      64,            /* bitsize */
  426.      true,            /* pc_relative */
  427.      0,            /* bitpos */
  428.      complain_overflow_signed, /* complain_on_overflow */
  429.      0,            /* special_function */
  430.      "SREL64",        /* name */
  431.      false,            /* partial_inplace */
  432.      MINUS_ONE,        /* src_mask */
  433.      MINUS_ONE,        /* dst_mask */
  434.      false),        /* pcrel_offset */
  435.  
  436.   /* Push a value on the reloc evaluation stack.  */
  437.   HOWTO (ALPHA_R_OP_PUSH,    /* type */
  438.      0,            /* rightshift */
  439.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  440.      0,            /* bitsize */
  441.      false,            /* pc_relative */
  442.      0,            /* bitpos */
  443.      complain_overflow_dont, /* complain_on_overflow */
  444.      elf64_alpha_reloc_op_push, /* special_function */
  445.      "OP_PUSH",        /* name */
  446.      false,            /* partial_inplace */
  447.      0,            /* src_mask */
  448.      0,            /* dst_mask */
  449.      false),        /* pcrel_offset */
  450.  
  451.   /* Store the value from the stack at the given address.  Store it in
  452.      a bitfield of size r_size starting at bit position r_offset.  */
  453.   HOWTO (ALPHA_R_OP_STORE,    /* type */
  454.      0,            /* rightshift */
  455.      4,            /* size (0 = byte, 1 = short, 2 = long) */
  456.      64,            /* bitsize */
  457.      false,            /* pc_relative */
  458.      0,            /* bitpos */
  459.      complain_overflow_dont, /* complain_on_overflow */
  460.      elf64_alpha_reloc_op_store, /* special_function */
  461.      "OP_STORE",        /* name */
  462.      false,            /* partial_inplace */
  463.      0,            /* src_mask */
  464.      MINUS_ONE,        /* dst_mask */
  465.      false),        /* pcrel_offset */
  466.  
  467.   /* Subtract the reloc address from the value on the top of the
  468.      relocation stack.  */
  469.   HOWTO (ALPHA_R_OP_PSUB,    /* type */
  470.      0,            /* rightshift */
  471.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  472.      0,            /* bitsize */
  473.      false,            /* pc_relative */
  474.      0,            /* bitpos */
  475.      complain_overflow_dont, /* complain_on_overflow */
  476.      elf64_alpha_reloc_op_psub, /* special_function */
  477.      "OP_PSUB",        /* name */
  478.      false,            /* partial_inplace */
  479.      0,            /* src_mask */
  480.      0,            /* dst_mask */
  481.      false),        /* pcrel_offset */
  482.  
  483.   /* Shift the value on the top of the relocation stack right by the
  484.      given value.  */
  485.   HOWTO (ALPHA_R_OP_PRSHIFT,    /* type */
  486.      0,            /* rightshift */
  487.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  488.      0,            /* bitsize */
  489.      false,            /* pc_relative */
  490.      0,            /* bitpos */
  491.      complain_overflow_dont, /* complain_on_overflow */
  492.      elf64_alpha_reloc_op_prshift, /* special_function */
  493.      "OP_PRSHIFT",        /* name */
  494.      false,            /* partial_inplace */
  495.      0,            /* src_mask */
  496.      0,            /* dst_mask */
  497.      false),        /* pcrel_offset */
  498.  
  499.   /* Misc ELF relocations. */
  500.   HOWTO (R_ALPHA_COPY,
  501.      0,
  502.      0,
  503.      0,
  504.      false,
  505.      0,
  506.      complain_overflow_dont,
  507.      bfd_elf_generic_reloc,
  508.      "COPY",
  509.      false,
  510.      0,
  511.      0, 
  512.      true),
  513.  
  514.   HOWTO (R_ALPHA_GLOB_DAT,
  515.      0,
  516.      0,
  517.      0,
  518.      false,
  519.      0,
  520.      complain_overflow_dont,
  521.      bfd_elf_generic_reloc,
  522.      "GLOB_DAT",
  523.      false,
  524.      0,
  525.      0, 
  526.      true),
  527.  
  528.   HOWTO (R_ALPHA_JMP_SLOT,
  529.      0,
  530.      0,
  531.      0,
  532.      false,
  533.      0,
  534.      complain_overflow_dont,
  535.      bfd_elf_generic_reloc,
  536.      "JMP_SLOT",
  537.      false,
  538.      0,
  539.      0,
  540.      true),
  541.  
  542.   HOWTO (R_ALPHA_RELATIVE,
  543.      0,
  544.      0,
  545.      0,
  546.      false,
  547.      0,
  548.      complain_overflow_dont,
  549.      bfd_elf_generic_reloc,
  550.      "RELATIVE", 
  551.      false,
  552.      0,
  553.      0,
  554.      true)
  555. };
  556.  
  557. static bfd_reloc_status_type
  558. elf64_alpha_reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
  559.      bfd *abfd;
  560.      arelent *reloc;
  561.      asymbol *sym;
  562.      PTR data;
  563.      asection *sec;
  564.      bfd *output_bfd;
  565.      char **error_message;
  566. {
  567.   if (output_bfd)
  568.     reloc->address += sec->output_offset;
  569.   return bfd_reloc_ok;
  570. }
  571.  
  572. static bfd_reloc_status_type
  573. elf64_alpha_do_reloc_gpdisp (abfd, gpdisp, p_ldah, p_lda)
  574.      bfd *abfd;
  575.      bfd_vma gpdisp;
  576.      bfd_byte *p_ldah, *p_lda;
  577. {
  578.   bfd_reloc_status_type ret = bfd_reloc_ok;
  579.   bfd_vma addend;
  580.   unsigned long i_ldah, i_lda;
  581.  
  582.   i_ldah = bfd_get_32(abfd, p_ldah);
  583.   i_lda = bfd_get_32(abfd, p_lda);
  584.  
  585.   /* Complain if the instructions are not correct.  */
  586.   if (((i_ldah >> 26) & 0x3f) != 0x09
  587.       || ((i_lda >> 26) & 0x3f) != 0x08)
  588.     ret = bfd_reloc_dangerous;
  589.  
  590.   /* Extract the user-supplied offset, mirroring the sign extensions
  591.      that the instructions perform.  */
  592.   addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
  593.   addend = (addend ^ 0x80008000) - 0x80008000;
  594.  
  595.   gpdisp += addend;
  596.  
  597.   if ((bfd_signed_vma)gpdisp < -(bfd_signed_vma)0x80000000
  598.       || gpdisp >= 0x7fff8000)
  599.     ret = bfd_reloc_overflow;
  600.  
  601.   /* compensate for the sign extension again.  */
  602.   i_ldah = ((i_ldah & 0xffff0000)
  603.         | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
  604.   i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
  605.  
  606.   bfd_put_32 (abfd, i_ldah, p_ldah);
  607.   bfd_put_32 (abfd, i_lda, p_lda);
  608.  
  609.   return ret;
  610. }
  611.  
  612. static bfd_reloc_status_type
  613. elf64_alpha_reloc_gpdisp (abfd, reloc_entry, sym, data, input_section,
  614.               output_bfd, err_msg)
  615.      bfd *abfd;
  616.      arelent *reloc_entry;
  617.      asymbol *sym;
  618.      PTR data;
  619.      asection *input_section;
  620.      bfd *output_bfd;
  621.      char **err_msg;
  622. {
  623.   bfd_reloc_status_type ret;
  624.   bfd_vma gp, relocation;
  625.   bfd_byte *p_ldah, *p_lda;
  626.  
  627.   /* Don't do anything if we're not doing a final link.  */
  628.   if (output_bfd)
  629.     {
  630.       reloc_entry->address += input_section->output_offset;
  631.       return bfd_reloc_ok;
  632.     }
  633.  
  634.   if (reloc_entry->address > input_section->_cooked_size ||
  635.       reloc_entry->address + reloc_entry->addend > input_section->_cooked_size)
  636.     return bfd_reloc_outofrange;
  637.  
  638.   /* The gp used in the portion of the output object to which this 
  639.      input object belongs is cached on the input bfd.  */
  640.   gp = _bfd_get_gp_value (abfd);
  641.  
  642.   relocation = (input_section->output_section->vma
  643.         + input_section->output_offset
  644.         + reloc_entry->address);
  645.  
  646.   p_ldah = (bfd_byte *)data + reloc_entry->address;
  647.   p_lda = p_ldah + reloc_entry->addend;
  648.  
  649.   ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
  650.  
  651.   /* Complain if the instructions are not correct.  */
  652.   if (ret == bfd_reloc_dangerous)
  653.     {
  654.       *err_msg = "GPDISP relocation did not find ldah and lda instructions";
  655.     }
  656.  
  657.   return ret;
  658. }
  659.  
  660. /* Due to the nature of the stack operations, I don't think more
  661.    that one entry is useful.  Test this theory by setting the
  662.    stack size to a minimum.  */
  663. /* FIXME: BFD should not use static variables.  */
  664. #define OP_STACK_SIZE 1
  665. static bfd_vma elf64_alpha_op_stack[OP_STACK_SIZE];
  666. static int elf64_alpha_op_tos;
  667.  
  668. static bfd_reloc_status_type
  669. elf64_alpha_reloc_op_push (abfd, reloc_entry, sym, data, input_section,
  670.                output_bfd, err_msg)
  671.      bfd *abfd;
  672.      arelent *reloc_entry;
  673.      asymbol *sym;
  674.      PTR data;
  675.      asection *input_section;
  676.      bfd *output_bfd;
  677.      char **err_msg;
  678. {
  679.   bfd_reloc_status_type r = bfd_reloc_ok;
  680.   bfd_vma value;
  681.  
  682.   /* Don't do anything if we're not doing a final link.  */
  683.   if (output_bfd)
  684.     {
  685.       reloc_entry->address += input_section->output_offset;
  686.       return bfd_reloc_ok;
  687.     }
  688.  
  689.   if (elf64_alpha_op_tos >= OP_STACK_SIZE)
  690.     {
  691.       *err_msg = "operation stack overflow";
  692.       return bfd_reloc_dangerous;
  693.     }
  694.  
  695.   /* Get the symbol value.  */
  696.   /* FIXME: We should fail if this is a dynamic symbol.  Check on that.  */
  697.   if (bfd_is_und_section (sym->section))
  698.     r = bfd_reloc_undefined;
  699.   if (bfd_is_com_section (sym->section))
  700.     value = 0;
  701.   else
  702.     value = sym->value;
  703.   value += sym->section->output_section->vma;
  704.   value += sym->section->output_offset;
  705.   value += reloc_entry->addend;
  706.  
  707.   elf64_alpha_op_stack[elf64_alpha_op_tos++] = value;
  708.  
  709.   return r;
  710. }
  711.  
  712. static bfd_reloc_status_type
  713. elf64_alpha_reloc_op_store (abfd, reloc_entry, sym, data, input_section,
  714.                 output_bfd, err_msg)
  715.      bfd *abfd;
  716.      arelent *reloc_entry;
  717.      asymbol *sym;
  718.      PTR data;
  719.      asection *input_section;
  720.      bfd *output_bfd;
  721.      char **err_msg;
  722. {
  723.   int size, offset;
  724.   bfd_vma value;
  725.  
  726.   /* Don't do anything before the final link.  */
  727.   if (output_bfd)
  728.     {
  729.       reloc_entry->address += input_section->output_offset;
  730.       return bfd_reloc_ok;
  731.     }
  732.  
  733.   if (elf64_alpha_op_tos <= 0)
  734.     {
  735.       *err_msg = "operation stack underflow";
  736.       return bfd_reloc_dangerous;
  737.     }
  738.  
  739.   /* The offset and size for this reloc are encoded into the addend
  740.      field by alpha_adjust_reloc_in.  */
  741.   offset = (reloc_entry->addend >> 8) & 0xff;
  742.   size = reloc_entry->addend & 0xff;
  743.  
  744.   value = bfd_get_64 (abfd, data + reloc_entry->address);
  745.   value &= ~((((bfd_vma)1 << size) - 1) << offset);
  746.   value |= (elf64_alpha_op_stack[--elf64_alpha_op_tos] 
  747.         & (((bfd_vma)1 << size) - 1)) << offset;
  748.   bfd_put_64 (abfd, value, data + reloc_entry->address);
  749.  
  750.   return bfd_reloc_ok;
  751. }
  752.  
  753. static bfd_reloc_status_type
  754. elf64_alpha_reloc_op_psub (abfd, reloc_entry, sym, data, input_section,
  755.                output_bfd, err_msg)
  756.      bfd *abfd;
  757.      arelent *reloc_entry;
  758.      asymbol *sym;
  759.      PTR data;
  760.      asection *input_section;
  761.      bfd *output_bfd;
  762.      char **err_msg;
  763. {
  764.   bfd_reloc_status_type r;
  765.   bfd_vma value;
  766.  
  767.   /* Don't do anything before the final link.  */
  768.   if (output_bfd)
  769.     {
  770.       reloc_entry->address += input_section->output_offset;
  771.       return bfd_reloc_ok;
  772.     }
  773.  
  774.   if (elf64_alpha_op_tos <= 0)
  775.     {
  776.       *err_msg = "operation stack underflow";
  777.       return bfd_reloc_dangerous;
  778.     }
  779.  
  780.   if (bfd_is_und_section (sym->section))
  781.     r = bfd_reloc_undefined;
  782.   if (bfd_is_com_section (sym->section))
  783.     value = 0;
  784.   else
  785.     value = sym->value;
  786.   value += sym->section->output_section->vma;
  787.   value += sym->section->output_offset;
  788.   value += reloc_entry->addend;
  789.  
  790.   elf64_alpha_op_stack[elf64_alpha_op_tos-1] -= value;
  791.  
  792.   return r;
  793. }
  794.  
  795. static bfd_reloc_status_type
  796. elf64_alpha_reloc_op_prshift (abfd, reloc_entry, sym, data, input_section,
  797.                   output_bfd, err_msg)
  798.      bfd *abfd;
  799.      arelent *reloc_entry;
  800.      asymbol *sym;
  801.      PTR data;
  802.      asection *input_section;
  803.      bfd *output_bfd;
  804.      char **err_msg;
  805. {
  806.   /* Don't do anything before the final link.  */
  807.   if (output_bfd)
  808.     {
  809.       reloc_entry->address += input_section->output_offset;
  810.       return bfd_reloc_ok;
  811.     }
  812.  
  813.   if (elf64_alpha_op_tos <= 0)
  814.     {
  815.       *err_msg = "operation stack underflow";
  816.       return bfd_reloc_dangerous;
  817.     }
  818.  
  819.   elf64_alpha_op_stack[elf64_alpha_op_tos-1] >>= reloc_entry->addend;
  820.  
  821.   return bfd_reloc_ok;
  822. }
  823.  
  824. /* A mapping from BFD reloc types to Alpha ELF reloc types.  */
  825.  
  826. struct elf_reloc_map
  827. {
  828.   bfd_reloc_code_real_type bfd_reloc_val;
  829.   int elf_reloc_val;
  830. };
  831.  
  832. static const struct elf_reloc_map elf64_alpha_reloc_map[] =
  833. {
  834.   {BFD_RELOC_NONE,        R_ALPHA_NONE},
  835.   {BFD_RELOC_32,        R_ALPHA_REFLONG},
  836.   {BFD_RELOC_64,        R_ALPHA_REFQUAD},
  837.   {BFD_RELOC_CTOR,        R_ALPHA_REFQUAD},
  838.   {BFD_RELOC_GPREL32,        R_ALPHA_GPREL32},
  839.   {BFD_RELOC_ALPHA_LITERAL,    R_ALPHA_LITERAL},
  840.   {BFD_RELOC_ALPHA_LITUSE,    R_ALPHA_LITUSE},
  841.   {BFD_RELOC_ALPHA_GPDISP,    R_ALPHA_GPDISP},
  842.   {BFD_RELOC_23_PCREL_S2,    R_ALPHA_BRADDR},  
  843.   {BFD_RELOC_ALPHA_HINT,    R_ALPHA_HINT},  
  844.   {BFD_RELOC_16_PCREL,        R_ALPHA_SREL16},  
  845.   {BFD_RELOC_32_PCREL,        R_ALPHA_SREL32},  
  846.   {BFD_RELOC_64_PCREL,        R_ALPHA_SREL64},  
  847. #if 0
  848.   {BFD_RELOC_ALPHA_OP_PUSH,    R_ALPHA_OP_PUSH},
  849.   {BFD_RELOC_ALPHA_OP_STORE,    R_ALPHA_OP_STORE},
  850.   {BFD_RELOC_ALPHA_OP_PSUB,    R_ALPHA_OP_PSUB},
  851.   {BFD_RELOC_ALPHA_OP_PRSHIFT,    R_ALPHA_OP_PRSHIFT}
  852. #endif
  853. };
  854.  
  855. /* Given a BFD reloc type, return a HOWTO structure.  */
  856.  
  857. static reloc_howto_type *
  858. elf64_alpha_bfd_reloc_type_lookup (abfd, code)
  859.      bfd *abfd;
  860.      bfd_reloc_code_real_type code;
  861. {
  862.   const struct elf_reloc_map *i, *e;
  863.   i = e = elf64_alpha_reloc_map;
  864.   e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
  865.   for (; i != e; ++i)
  866.     {
  867.       if (i->bfd_reloc_val == code)
  868.     return &elf64_alpha_howto_table[i->elf_reloc_val];
  869.     }
  870.   return 0;
  871. }
  872.  
  873. /* Given an Alpha ELF reloc type, fill in an arelent structure.  */
  874.  
  875. static void
  876. elf64_alpha_info_to_howto (abfd, cache_ptr, dst)
  877.      bfd *abfd;
  878.      arelent *cache_ptr;
  879.      Elf64_Internal_Rela *dst;
  880. {
  881.   unsigned r_type;
  882.  
  883.   r_type = ELF64_R_TYPE(dst->r_info);
  884.   BFD_ASSERT (r_type < (unsigned int) R_ALPHA_max);
  885.   cache_ptr->howto = &elf64_alpha_howto_table[r_type];
  886. }
  887.  
  888. /* PLT/GOT Stuff */
  889. #define PLT_HEADER_SIZE 32
  890. #define PLT_HEADER_WORD1    0xc3600000    /* br   $27,.+4     */
  891. #define PLT_HEADER_WORD2    0xa77b000c    /* ldq  $27,12($27) */
  892. #define PLT_HEADER_WORD3    0x47ff041f    /* nop              */
  893. #define PLT_HEADER_WORD4    0x6b7b0000    /* jmp  $27,($27)   */
  894.  
  895. #define PLT_ENTRY_SIZE 12
  896. #define PLT_ENTRY_WORD1        0x279f0000    /* ldah $28, 0($31) */
  897. #define PLT_ENTRY_WORD2        0x239c0000    /* lda  $28, 0($28) */
  898. #define PLT_ENTRY_WORD3        0xc3e00000    /* br   $31, plt0   */
  899.  
  900. #define RESERVED_GOT_ENTRIES 1
  901.  
  902. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
  903.  
  904. /* Set the right machine number for an Alpha ELF file.  */
  905.  
  906. static boolean
  907. elf64_alpha_object_p (abfd)
  908.      bfd *abfd;
  909. {
  910.   return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
  911. }
  912.  
  913. /* Handle a alpha specific section when reading an object file.  This
  914.    is called when elfcode.h finds a section with an unknown type.
  915.    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
  916.    how to.  */
  917.  
  918. static boolean
  919. elf64_alpha_section_from_shdr (abfd, hdr, name)
  920.      bfd *abfd;
  921.      Elf64_Internal_Shdr *hdr;
  922.      char *name;
  923. {
  924.   asection *newsect;
  925.  
  926.   /* There ought to be a place to keep ELF backend specific flags, but
  927.      at the moment there isn't one.  We just keep track of the
  928.      sections by their name, instead.  Fortunately, the ABI gives
  929.      suggested names for all the MIPS specific sections, so we will
  930.      probably get away with this.  */
  931.   switch (hdr->sh_type)
  932.     {
  933.     case SHT_ALPHA_DEBUG:
  934.       if (strcmp (name, ".mdebug") != 0)
  935.     return false;
  936.       break;
  937. #ifdef ERIC_neverdef
  938.     case SHT_ALPHA_REGINFO:
  939.       if (strcmp (name, ".reginfo") != 0
  940.       || hdr->sh_size != sizeof (Elf64_External_RegInfo))
  941.     return false;
  942.       break;
  943. #endif
  944.     default:
  945.       return false;
  946.     }
  947.  
  948.   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
  949.     return false;
  950.   newsect = hdr->bfd_section;
  951.  
  952.   if (hdr->sh_type == SHT_ALPHA_DEBUG)
  953.     {
  954.       if (! bfd_set_section_flags (abfd, newsect,
  955.                    (bfd_get_section_flags (abfd, newsect)
  956.                     | SEC_DEBUGGING)))
  957.     return false;
  958.     }
  959.  
  960. #ifdef ERIC_neverdef
  961.   /* For a .reginfo section, set the gp value in the tdata information
  962.      from the contents of this section.  We need the gp value while
  963.      processing relocs, so we just get it now.  */
  964.   if (hdr->sh_type == SHT_ALPHA_REGINFO)
  965.     {
  966.       Elf64_External_RegInfo ext;
  967.       Elf64_RegInfo s;
  968.  
  969.       if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
  970.                       (file_ptr) 0, sizeof ext))
  971.     return false;
  972.       bfd_alpha_elf64_swap_reginfo_in (abfd, &ext, &s);
  973.       elf_gp (abfd) = s.ri_gp_value;
  974.     }
  975. #endif
  976.  
  977.   return true;
  978. }
  979.  
  980. /* Set the correct type for an Alpha ELF section.  We do this by the
  981.    section name, which is a hack, but ought to work.  */
  982.  
  983. static boolean
  984. elf64_alpha_fake_sections (abfd, hdr, sec)
  985.      bfd *abfd;
  986.      Elf64_Internal_Shdr *hdr;
  987.      asection *sec;
  988. {
  989.   register const char *name;
  990.  
  991.   name = bfd_get_section_name (abfd, sec);
  992.  
  993.   if (strcmp (name, ".mdebug") == 0)
  994.     {
  995.       hdr->sh_type = SHT_ALPHA_DEBUG;
  996.       /* In a shared object on Irix 5.3, the .mdebug section has an
  997.          entsize of 0.  FIXME: Does this matter?  */
  998.       if ((abfd->flags & DYNAMIC) != 0 )
  999.     hdr->sh_entsize = 0;
  1000.       else
  1001.     hdr->sh_entsize = 1;
  1002.     }
  1003. #ifdef ERIC_neverdef
  1004.   else if (strcmp (name, ".reginfo") == 0)
  1005.     {
  1006.       hdr->sh_type = SHT_ALPHA_REGINFO;
  1007.       /* In a shared object on Irix 5.3, the .reginfo section has an
  1008.          entsize of 0x18.  FIXME: Does this matter?  */
  1009.       if ((abfd->flags & DYNAMIC) != 0)
  1010.     hdr->sh_entsize = sizeof (Elf64_External_RegInfo);
  1011.       else
  1012.     hdr->sh_entsize = 1;
  1013.  
  1014.       /* Force the section size to the correct value, even if the
  1015.      linker thinks it is larger.  The link routine below will only
  1016.      write out this much data for .reginfo.  */
  1017.       hdr->sh_size = sec->_raw_size = sizeof (Elf64_External_RegInfo);
  1018.     }
  1019.   else if (strcmp (name, ".hash") == 0
  1020.        || strcmp (name, ".dynamic") == 0
  1021.        || strcmp (name, ".dynstr") == 0)
  1022.     {
  1023.       hdr->sh_entsize = 0;
  1024.       hdr->sh_info = SIZEOF_ALPHA_DYNSYM_SECNAMES;
  1025.     }
  1026.   else if (strcmp (name, ".sdata") == 0
  1027.        || strcmp (name, ".sbss") == 0
  1028.        || strcmp (name, ".lit4") == 0
  1029.        || strcmp (name, ".lit8") == 0)
  1030.     hdr->sh_flags |= SHF_ALPHA_GPREL;
  1031. #endif
  1032.  
  1033.   return true;
  1034. }
  1035.  
  1036. static int
  1037. elf64_alpha_additional_program_headers (abfd)
  1038.      bfd *abfd;
  1039. {
  1040.   asection *s;
  1041.   int ret;
  1042.  
  1043.   ret = 0;
  1044.  
  1045.   s = bfd_get_section_by_name (abfd, ".reginfo");
  1046.   if (s != NULL && (s->flags & SEC_LOAD) != 0)
  1047.     {
  1048.       /* We need a PT_ALPHA_REGINFO segment.  */
  1049.       ++ret;
  1050.     }
  1051.  
  1052.   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
  1053.       && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
  1054.     {
  1055.       /* We need a PT_ALPHA_RTPROC segment.  */
  1056.       ++ret;
  1057.     }
  1058.  
  1059.   return ret;
  1060. }
  1061.  
  1062. static boolean
  1063. elf64_alpha_create_got_section(abfd, info)
  1064.      bfd *abfd;
  1065.      struct bfd_link_info *info;
  1066. {
  1067.   asection *s;
  1068.   struct elf_link_hash_entry *h;
  1069.  
  1070.   if (bfd_get_section_by_name (abfd, ".got"))
  1071.     return true;
  1072.  
  1073.   s = bfd_make_section(abfd, ".rela.got");
  1074.   if (s == NULL
  1075.       || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
  1076.                        | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1077.                        | SEC_READONLY))
  1078.       || !bfd_set_section_alignment (abfd, s, 3))
  1079.     return false;
  1080.  
  1081.   s = bfd_make_section(abfd, ".got");
  1082.   if (s == NULL
  1083.       || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
  1084.                        | SEC_HAS_CONTENTS | SEC_IN_MEMORY))
  1085.       || !bfd_set_section_alignment (abfd, s, 3))
  1086.     return false;
  1087.  
  1088.   s->_raw_size = RESERVED_GOT_ENTRIES * 8;
  1089.  
  1090.   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
  1091.      (or .got.plt) section.  We don't do this in the linker script
  1092.      because we don't want to define the symbol if we are not creating
  1093.      a global offset table.  */
  1094.   h = NULL;
  1095.   if (!(_bfd_generic_link_add_one_symbol
  1096.     (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0,
  1097.      (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
  1098.      (struct bfd_link_hash_entry **) &h)))
  1099.     return false;
  1100.   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  1101.   h->type = STT_OBJECT;
  1102.  
  1103.   if (info->shared
  1104.       && ! _bfd_elf_link_record_dynamic_symbol (info, h))
  1105.     return false;
  1106.  
  1107.   elf_hash_table (info)->hgot = h;
  1108.  
  1109.   return true;
  1110. }
  1111.  
  1112. static boolean
  1113. elf64_alpha_create_dynamic_sections (abfd, info)
  1114.      bfd *abfd;
  1115.      struct bfd_link_info *info;
  1116. {
  1117.   register asection *s;
  1118.   struct elf_link_hash_entry *h;
  1119.  
  1120.   /* We need to create .plt, .rela.plt, .got, and .rela.got sections.  */
  1121.  
  1122.   s = bfd_make_section (abfd, ".plt");
  1123.   if (s == NULL
  1124.       || ! bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
  1125.                         | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1126.                         | SEC_CODE))
  1127.       || ! bfd_set_section_alignment (abfd, s, 3))
  1128.     return false;
  1129.  
  1130.   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
  1131.      .plt section.  */
  1132.   h = NULL;
  1133.   if (! (_bfd_generic_link_add_one_symbol
  1134.      (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
  1135.       (bfd_vma) 0, (const char *) NULL, false,
  1136.       get_elf_backend_data (abfd)->collect,
  1137.       (struct bfd_link_hash_entry **) &h)))
  1138.     return false;
  1139.   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  1140.   h->type = STT_OBJECT;
  1141.  
  1142.   if (info->shared
  1143.       && ! _bfd_elf_link_record_dynamic_symbol (info, h))
  1144.     return false;
  1145.  
  1146.   s = bfd_make_section (abfd, ".rela.plt");
  1147.   if (s == NULL
  1148.       || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
  1149.                       | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1150.                       | SEC_READONLY))
  1151.       || ! bfd_set_section_alignment (abfd, s, 3))
  1152.     return false;
  1153.  
  1154.   if (!elf64_alpha_create_got_section (abfd, info))
  1155.     return false;
  1156.  
  1157.   return true;
  1158. }
  1159.  
  1160. /* The structure of the runtile procedure descriptor created by the
  1161.    loader for use by the static exception system.  */
  1162.  
  1163. /* FIXME */
  1164.  
  1165. /* Read ECOFF debugging information from a .mdebug section into a
  1166.    ecoff_debug_info structure.  */
  1167.  
  1168. static boolean
  1169. elf64_alpha_read_ecoff_info (abfd, section, debug)
  1170.      bfd *abfd;
  1171.      asection *section;
  1172.      struct ecoff_debug_info *debug;
  1173. {
  1174.   HDRR *symhdr;
  1175.   const struct ecoff_debug_swap *swap;
  1176.   char *ext_hdr = NULL;
  1177.  
  1178.   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  1179.  
  1180.   ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
  1181.   if (ext_hdr == NULL && swap->external_hdr_size != 0)
  1182.     goto error_return;
  1183.  
  1184.   if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
  1185.                 swap->external_hdr_size)
  1186.       == false)
  1187.     goto error_return;
  1188.  
  1189.   symhdr = &debug->symbolic_header;
  1190.   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
  1191.  
  1192.   /* The symbolic header contains absolute file offsets and sizes to
  1193.      read.  */
  1194. #define READ(ptr, offset, count, size, type)                \
  1195.   if (symhdr->count == 0)                        \
  1196.     debug->ptr = NULL;                            \
  1197.   else                                    \
  1198.     {                                    \
  1199.       debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count));    \
  1200.       if (debug->ptr == NULL)                        \
  1201.     goto error_return;                        \
  1202.       if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0    \
  1203.       || (bfd_read (debug->ptr, size, symhdr->count,        \
  1204.             abfd) != size * symhdr->count))            \
  1205.     goto error_return;                        \
  1206.     }
  1207.  
  1208.   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
  1209.   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
  1210.   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
  1211.   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
  1212.   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
  1213.   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
  1214.     union aux_ext *);
  1215.   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
  1216.   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
  1217.   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
  1218.   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
  1219.   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
  1220. #undef READ
  1221.  
  1222.   debug->fdr = NULL;
  1223.   debug->adjust = NULL;
  1224.  
  1225.   return true;
  1226.  
  1227.  error_return:
  1228.   if (ext_hdr != NULL)
  1229.     free (ext_hdr);
  1230.   if (debug->line != NULL)
  1231.     free (debug->line);
  1232.   if (debug->external_dnr != NULL)
  1233.     free (debug->external_dnr);
  1234.   if (debug->external_pdr != NULL)
  1235.     free (debug->external_pdr);
  1236.   if (debug->external_sym != NULL)
  1237.     free (debug->external_sym);
  1238.   if (debug->external_opt != NULL)
  1239.     free (debug->external_opt);
  1240.   if (debug->external_aux != NULL)
  1241.     free (debug->external_aux);
  1242.   if (debug->ss != NULL)
  1243.     free (debug->ss);
  1244.   if (debug->ssext != NULL)
  1245.     free (debug->ssext);
  1246.   if (debug->external_fdr != NULL)
  1247.     free (debug->external_fdr);
  1248.   if (debug->external_rfd != NULL)
  1249.     free (debug->external_rfd);
  1250.   if (debug->external_ext != NULL)
  1251.     free (debug->external_ext);
  1252.   return false;
  1253. }
  1254.  
  1255. /* Alpha ELF local labels start with '$'.  */
  1256.  
  1257. static boolean
  1258. elf64_alpha_is_local_label (abfd, symbol)
  1259.      bfd *abfd;
  1260.      asymbol *symbol;
  1261. {
  1262.   return symbol->name[0] == '$';
  1263. }
  1264.  
  1265. /* Alpha ELF follows MIPS ELF in using a special find_nearest_line
  1266.    routine in order to handle the ECOFF debugging information.  We
  1267.    still call this mips_elf_find_line because of the slot
  1268.    find_line_info in elf_obj_tdata is declared that way.  */
  1269.  
  1270. struct mips_elf_find_line
  1271. {
  1272.   struct ecoff_debug_info d;
  1273.   struct ecoff_find_line i;
  1274. };
  1275.  
  1276. static boolean
  1277. elf64_alpha_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
  1278.                    functionname_ptr, line_ptr)
  1279.      bfd *abfd;
  1280.      asection *section;
  1281.      asymbol **symbols;
  1282.      bfd_vma offset;
  1283.      const char **filename_ptr;
  1284.      const char **functionname_ptr;
  1285.      unsigned int *line_ptr;
  1286. {
  1287.   asection *msec;
  1288.  
  1289.   msec = bfd_get_section_by_name (abfd, ".mdebug");
  1290.   if (msec != NULL)
  1291.     {
  1292.       flagword origflags;
  1293.       struct mips_elf_find_line *fi;
  1294.       const struct ecoff_debug_swap * const swap =
  1295.     get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  1296.  
  1297.       /* If we are called during a link, alpha_elf_final_link may have
  1298.      cleared the SEC_HAS_CONTENTS field.  We force it back on here
  1299.      if appropriate (which it normally will be).  */
  1300.       origflags = msec->flags;
  1301.       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
  1302.     msec->flags |= SEC_HAS_CONTENTS;
  1303.  
  1304.       fi = elf_tdata (abfd)->find_line_info;
  1305.       if (fi == NULL)
  1306.     {
  1307.       bfd_size_type external_fdr_size;
  1308.       char *fraw_src;
  1309.       char *fraw_end;
  1310.       struct fdr *fdr_ptr;
  1311.  
  1312.       fi = ((struct mips_elf_find_line *)
  1313.         bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
  1314.       if (fi == NULL)
  1315.         {
  1316.           msec->flags = origflags;
  1317.           return false;
  1318.         }
  1319.  
  1320.       if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
  1321.         {
  1322.           msec->flags = origflags;
  1323.           return false;
  1324.         }
  1325.  
  1326.       /* Swap in the FDR information.  */
  1327.       fi->d.fdr = ((struct fdr *)
  1328.                bfd_alloc (abfd,
  1329.                   (fi->d.symbolic_header.ifdMax *
  1330.                    sizeof (struct fdr))));
  1331.       if (fi->d.fdr == NULL)
  1332.         {
  1333.           msec->flags = origflags;
  1334.           return false;
  1335.         }
  1336.       external_fdr_size = swap->external_fdr_size;
  1337.       fdr_ptr = fi->d.fdr;
  1338.       fraw_src = (char *) fi->d.external_fdr;
  1339.       fraw_end = (fraw_src
  1340.               + fi->d.symbolic_header.ifdMax * external_fdr_size);
  1341.       for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
  1342.         (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
  1343.  
  1344.       elf_tdata (abfd)->find_line_info = fi;
  1345.  
  1346.       /* Note that we don't bother to ever free this information.
  1347.              find_nearest_line is either called all the time, as in
  1348.              objdump -l, so the information should be saved, or it is
  1349.              rarely called, as in ld error messages, so the memory
  1350.              wasted is unimportant.  Still, it would probably be a
  1351.              good idea for free_cached_info to throw it away.  */
  1352.     }
  1353.  
  1354.       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
  1355.                   &fi->i, filename_ptr, functionname_ptr,
  1356.                   line_ptr))
  1357.     {
  1358.       msec->flags = origflags;
  1359.       return true;
  1360.     }
  1361.  
  1362.       msec->flags = origflags;
  1363.     }
  1364.  
  1365.   /* Fall back on the generic ELF find_nearest_line routine.  */
  1366.  
  1367.   return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
  1368.                      filename_ptr, functionname_ptr,
  1369.                      line_ptr);
  1370. }
  1371.  
  1372. /* Structure used to pass information to alpha_elf_output_extsym.  */
  1373.  
  1374. struct extsym_info
  1375. {
  1376.   bfd *abfd;
  1377.   struct bfd_link_info *info;
  1378.   struct ecoff_debug_info *debug;
  1379.   const struct ecoff_debug_swap *swap;
  1380.   boolean failed;
  1381. };
  1382.  
  1383. static boolean
  1384. elf64_alpha_output_extsym (h, data)
  1385.      struct alpha_elf_link_hash_entry *h;
  1386.      PTR data;
  1387. {
  1388.   struct extsym_info *einfo = (struct extsym_info *) data;
  1389.   boolean strip;
  1390.   asection *sec, *output_section;
  1391.  
  1392.   if (h->root.indx == -2)
  1393.     strip = false;
  1394.   else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  1395.            || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
  1396.           && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
  1397.           && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
  1398.     strip = true;
  1399.   else if (einfo->info->strip == strip_all
  1400.           || (einfo->info->strip == strip_some
  1401.               && bfd_hash_lookup (einfo->info->keep_hash,
  1402.                                   h->root.root.root.string,
  1403.                                   false, false) == NULL))
  1404.     strip = true;
  1405.   else
  1406.     strip = false;
  1407.  
  1408.   if (strip)
  1409.     return true;
  1410.  
  1411.   if (h->esym.ifd == -2)
  1412.     {
  1413.       h->esym.jmptbl = 0;
  1414.       h->esym.cobol_main = 0;
  1415.       h->esym.weakext = 0;
  1416.       h->esym.reserved = 0;
  1417.       h->esym.ifd = ifdNil;
  1418.       h->esym.asym.value = 0;
  1419.       h->esym.asym.st = stGlobal;
  1420.  
  1421.       if (h->root.root.type != bfd_link_hash_defined
  1422.          && h->root.root.type != bfd_link_hash_defweak)
  1423.        h->esym.asym.sc = scAbs;
  1424.       else
  1425.        {
  1426.          const char *name;
  1427.  
  1428.          sec = h->root.root.u.def.section;
  1429.          output_section = sec->output_section;
  1430.  
  1431.          /* When making a shared library and symbol h is the one from
  1432.             the another shared library, OUTPUT_SECTION may be null.  */
  1433.          if (output_section == NULL)
  1434.            h->esym.asym.sc = scUndefined;
  1435.          else
  1436.            {
  1437.              name = bfd_section_name (output_section->owner, output_section);
  1438.        
  1439.              if (strcmp (name, ".text") == 0)
  1440.                h->esym.asym.sc = scText;
  1441.              else if (strcmp (name, ".data") == 0)
  1442.                h->esym.asym.sc = scData;
  1443.              else if (strcmp (name, ".sdata") == 0)
  1444.                h->esym.asym.sc = scSData;
  1445.              else if (strcmp (name, ".rodata") == 0
  1446.                       || strcmp (name, ".rdata") == 0)
  1447.                h->esym.asym.sc = scRData;
  1448.              else if (strcmp (name, ".bss") == 0)
  1449.                h->esym.asym.sc = scBss;
  1450.              else if (strcmp (name, ".sbss") == 0)
  1451.                h->esym.asym.sc = scSBss;
  1452.              else if (strcmp (name, ".init") == 0)
  1453.                h->esym.asym.sc = scInit;
  1454.              else if (strcmp (name, ".fini") == 0)
  1455.                h->esym.asym.sc = scFini;
  1456.              else
  1457.                h->esym.asym.sc = scAbs;
  1458.            }
  1459.        }
  1460.  
  1461.       h->esym.asym.reserved = 0;
  1462.       h->esym.asym.index = indexNil;
  1463.     }
  1464.  
  1465.   if (h->root.root.type == bfd_link_hash_common)
  1466.     h->esym.asym.value = h->root.root.u.c.size;
  1467.   else if (h->root.root.type == bfd_link_hash_defined
  1468.        || h->root.root.type == bfd_link_hash_defweak)
  1469.     {
  1470.       if (h->esym.asym.sc == scCommon)
  1471.        h->esym.asym.sc = scBss;
  1472.       else if (h->esym.asym.sc == scSCommon)
  1473.        h->esym.asym.sc = scSBss;
  1474.  
  1475.       sec = h->root.root.u.def.section;
  1476.       output_section = sec->output_section;
  1477.       if (output_section != NULL)
  1478.        h->esym.asym.value = (h->root.root.u.def.value
  1479.                              + sec->output_offset
  1480.                              + output_section->vma);
  1481.       else
  1482.        h->esym.asym.value = 0;
  1483.     }
  1484.   else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
  1485.     {
  1486.       /* Set type and value for a symbol with a function stub.  */
  1487.       h->esym.asym.st = stProc;
  1488.       sec = h->root.root.u.def.section;
  1489.       if (sec == NULL)
  1490.        h->esym.asym.value = 0;
  1491.       else
  1492.        {
  1493.          output_section = sec->output_section;
  1494.          if (output_section != NULL)
  1495.            h->esym.asym.value = (h->root.plt_offset
  1496.                                  + sec->output_offset
  1497.                                  + output_section->vma);
  1498.          else
  1499.            h->esym.asym.value = 0;
  1500.        }
  1501. #if 0 /* FIXME?  */
  1502.       h->esym.ifd = 0;
  1503. #endif
  1504.     }      
  1505.  
  1506.   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
  1507.                                      h->root.root.root.string,
  1508.                                      &h->esym))
  1509.     {
  1510.       einfo->failed = true;
  1511.       return false;
  1512.     }
  1513.  
  1514.   return true;
  1515. }
  1516.  
  1517. /* FIXME:  Create a runtime procedure table from the .mdebug section.
  1518.  
  1519. static boolean
  1520. mips_elf_create_procedure_table (handle, abfd, info, s, debug)
  1521.      PTR handle;
  1522.      bfd *abfd;
  1523.      struct bfd_link_info *info;
  1524.      asection *s;
  1525.      struct ecoff_debug_info *debug;
  1526.  */
  1527.  
  1528.  
  1529. static boolean
  1530. elf64_alpha_check_relocs (abfd, info, sec, relocs)
  1531.      bfd *abfd;
  1532.      struct bfd_link_info *info;
  1533.      asection *sec;
  1534.      const Elf_Internal_Rela *relocs;
  1535. {
  1536.   bfd *dynobj;
  1537.   asection *sgot;
  1538.   asection *srelgot;
  1539.   asection *sreloc;
  1540.   Elf_Internal_Shdr *symtab_hdr;
  1541.   struct elf_link_hash_entry **sym_hashes;
  1542.   const Elf_Internal_Rela *rel, *relend;
  1543.  
  1544.   if (info->relocateable)
  1545.     return true;
  1546.  
  1547.   sgot = srelgot = sreloc = NULL;
  1548.   symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
  1549.   sym_hashes = elf_sym_hashes(abfd);
  1550.   dynobj = elf_hash_table(info)->dynobj;
  1551.   if (dynobj)
  1552.     {
  1553.       sgot = bfd_get_section_by_name(dynobj, ".got");
  1554.       srelgot = bfd_get_section_by_name(dynobj, ".rela.got");
  1555.     }
  1556.  
  1557.   relend = relocs + sec->reloc_count;
  1558.   for (rel = relocs; rel < relend; ++rel)
  1559.     {
  1560.       unsigned long r_symndx;
  1561.       struct alpha_elf_link_hash_entry *h;
  1562.  
  1563.       r_symndx = ELF64_R_SYM (rel->r_info);
  1564.       if (r_symndx < symtab_hdr->sh_info)
  1565.     h = NULL;
  1566.       else
  1567.     h = ((struct alpha_elf_link_hash_entry *)
  1568.          sym_hashes[r_symndx - symtab_hdr->sh_info]);
  1569.  
  1570.       switch (ELF64_R_TYPE (rel->r_info))
  1571.     {
  1572.     case R_ALPHA_LITERAL:
  1573.       /* If this is a load of a function symbol and we are building a
  1574.          shared library or calling a shared library, then we need a
  1575.          .plt entry as well. 
  1576.  
  1577.          We can tell if it is a function either by noticing the
  1578.          type of the symbol, or, if the type is undefined, by
  1579.          noticing that we have a LITUSE(3) reloc next.
  1580.  
  1581.          Note that it is not fatal to be wrong guessing that a symbol
  1582.          is an object, but it is fatal to be wrong guessing that a 
  1583.          symbol is a function.
  1584.  
  1585.          Furthermore, the .plt trampoline does not give constant 
  1586.          function addresses, so if we ever see a function's address
  1587.          taken, we cannot do lazy binding on that function. */
  1588.  
  1589.       if (h)
  1590.         {
  1591.           if (rel+1 < relend
  1592.           && ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE)
  1593.         {
  1594.           switch (rel[1].r_addend)
  1595.             {
  1596.             case 1: /* Memory reference */
  1597.               h->flags |= ALPHA_ELF_LINK_HASH_LU_MEM;
  1598.               break;
  1599.             case 3: /* Call reference */
  1600.               h->flags |= ALPHA_ELF_LINK_HASH_LU_FUNC;
  1601.               break;
  1602.             }
  1603.         }
  1604.           else
  1605.         h->flags |= ALPHA_ELF_LINK_HASH_LU_ADDR;
  1606.  
  1607.           if (h->root.root.type != bfd_link_hash_undefweak
  1608.           && (info->shared 
  1609.               || !(h->root.elf_link_hash_flags
  1610.                & ELF_LINK_HASH_DEF_REGULAR))
  1611.           && (h->root.type == STT_FUNC
  1612.               || (h->root.type == STT_NOTYPE
  1613.               && (h->flags & ALPHA_ELF_LINK_HASH_LU_FUNC))))
  1614.         {
  1615.           h->root.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
  1616.         }
  1617.         }
  1618.  
  1619.       if (dynobj == NULL)
  1620.         {
  1621.           elf_hash_table(info)->dynobj = dynobj = abfd;
  1622.  
  1623.           /* Create the .got section.  */
  1624.           if (!elf64_alpha_create_got_section(dynobj, info))
  1625.         return false;
  1626.  
  1627.           sgot = bfd_get_section_by_name(dynobj, ".got");
  1628.           srelgot = bfd_get_section_by_name(dynobj, ".rela.got");
  1629.         }
  1630.  
  1631.       if (h != NULL)
  1632.         {
  1633.           if (h->root.got_offset != MINUS_ONE)
  1634.         {
  1635.           /* We have already allocated space in this .got.  */
  1636.           break;
  1637.         }
  1638.  
  1639.           /* Make sure this becomes a dynamic symbol.  */
  1640.           if (h->root.dynindx == -1
  1641.           && ! _bfd_elf_link_record_dynamic_symbol (info, &h->root))
  1642.         return false;
  1643.  
  1644.           /* Reserve space for a reloc even if we won't use it.  */
  1645.           srelgot->_raw_size += sizeof(Elf64_External_Rela);
  1646.  
  1647.           /* Create the relocation in adjust_dynamic_symbol */
  1648.  
  1649.           h->root.got_offset = sgot->_raw_size;
  1650.           sgot->_raw_size += 8;
  1651.         }
  1652.       else
  1653.         {
  1654.           bfd_vma *lgotoff = elf_local_got_offsets(abfd);
  1655.           if (lgotoff == NULL)
  1656.         {
  1657.           size_t size;
  1658.  
  1659.           size = elf_tdata(abfd)->symtab_hdr.sh_info * sizeof(bfd_vma);
  1660.           lgotoff = (bfd_vma *)bfd_alloc(abfd, size);
  1661.           if (lgotoff == NULL)
  1662.             return false;
  1663.  
  1664.           elf_local_got_offsets(abfd) = lgotoff;
  1665.           memset(lgotoff, -1, size);
  1666.         }
  1667.  
  1668.           if (lgotoff[ELF64_R_SYM(rel->r_info)] != MINUS_ONE)
  1669.         {
  1670.           /* We have already allocated space in the .got.  */
  1671.           break;
  1672.         }
  1673.           lgotoff[ELF64_R_SYM(rel->r_info)] = sgot->_raw_size;
  1674.           sgot->_raw_size += 8;
  1675.  
  1676.           if (info->shared)
  1677.         {
  1678.           /* If we are generating a shared object, we need to
  1679.              output a R_ALPHA_RELATIVE reloc so that the dynamic
  1680.              linker can adjust this GOT entry.  */
  1681.           srelgot->_raw_size += sizeof(Elf64_External_Rela);
  1682.         }
  1683.         }
  1684.       break;
  1685.  
  1686.     case R_ALPHA_SREL16:
  1687.     case R_ALPHA_SREL32:
  1688.     case R_ALPHA_SREL64:
  1689.       if (h == NULL)
  1690.         break;
  1691.       /* FALLTHRU */
  1692.  
  1693.     case R_ALPHA_REFLONG:
  1694.     case R_ALPHA_REFQUAD:
  1695.       if (info->shared
  1696.           || (h != NULL
  1697.           && !(h->root.elf_link_hash_flags
  1698.                & ELF_LINK_HASH_DEF_REGULAR)))
  1699.         {
  1700.           /* When creating a shared object or referring to a symbol in
  1701.          a shared object, we must copy these relocs into the
  1702.          object file.  We create a reloc section in dynobj and
  1703.          make room for the reloc.  */
  1704.           if (sreloc == NULL)
  1705.         {
  1706.           const char *name;
  1707.           name = (bfd_elf_string_from_elf_section
  1708.               (abfd, elf_elfheader(abfd)->e_shstrndx,
  1709.                elf_section_data(sec)->rel_hdr.sh_name));
  1710.           if (name == NULL)
  1711.             return false;
  1712.  
  1713.           BFD_ASSERT (strncmp (name, ".rela", 5) == 0
  1714.                   && strcmp (bfd_get_section_name (abfd, sec),
  1715.                      name+5) == 0);
  1716.  
  1717.           sreloc = bfd_get_section_by_name (dynobj, name);
  1718.           if (sreloc == NULL)
  1719.             {
  1720.               sreloc = bfd_make_section (dynobj, name);
  1721.               if (sreloc == NULL
  1722.               || !bfd_set_section_flags (dynobj, sreloc,
  1723.                              (SEC_ALLOC|SEC_LOAD
  1724.                               |SEC_HAS_CONTENTS
  1725.                               |SEC_IN_MEMORY
  1726.                               |SEC_READONLY))
  1727.               || !bfd_set_section_alignment (dynobj, sreloc, 3))
  1728.             return false;
  1729.             }
  1730.         }
  1731.           sreloc->_raw_size += sizeof (Elf64_External_Rela);
  1732.         }
  1733.       break;
  1734.     }
  1735.     }
  1736.  
  1737.   return true;
  1738. }
  1739.  
  1740. /* Adjust a symbol defined by a dynamic object and referenced by a
  1741.    regular object.  The current definition is in some section of the
  1742.    dynamic object, but we're not including those sections.  We have to
  1743.    change the definition to something the rest of the link can
  1744.    understand.  */
  1745.  
  1746. static boolean
  1747. elf64_alpha_adjust_dynamic_symbol (info, h)
  1748.      struct bfd_link_info *info;
  1749.      struct elf_link_hash_entry *h;
  1750. {
  1751.   bfd *dynobj;
  1752.   asection *s;
  1753.   
  1754.   dynobj = elf_hash_table(info)->dynobj;
  1755.  
  1756.   /* If this is a function, put it in the procedure linkage table.  We
  1757.      will fill in the contents of the procedure linkage table later, 
  1758.      though we could actually do it here.  */
  1759.  
  1760.   if (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
  1761.     {
  1762.       /* We hadn't seen all of the input symbols or all of the relocations
  1763.      when we guessed that we needed a .plt entry.  Revise our decision.  */
  1764.       if ((!info->shared
  1765.        && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
  1766.       || (((struct alpha_elf_link_hash_entry *) h)->flags
  1767.           & ALPHA_ELF_LINK_HASH_LU_ADDR))
  1768.     {
  1769.       h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
  1770.       return true;
  1771.     }
  1772.  
  1773.       s = bfd_get_section_by_name(dynobj, ".plt");
  1774.       BFD_ASSERT(s != NULL);
  1775.  
  1776.       /* The first bit of the .plt is reserved.  */
  1777.       if (s->_raw_size == 0)
  1778.     s->_raw_size = PLT_HEADER_SIZE;
  1779.  
  1780.       h->plt_offset = s->_raw_size;
  1781.  
  1782.       /* If this symbol is not defined in a regular file, and we are not
  1783.      generating a shared library, then set the symbol to the location
  1784.      in the .plt.  This is required to make function pointers compare
  1785.      equal between the normal executable and the shared library.  */
  1786.       if (!info->shared)
  1787.     {
  1788.       h->root.u.def.section = s;
  1789.       h->root.u.def.value = s->_raw_size;
  1790.     }
  1791.  
  1792.       s->_raw_size += PLT_ENTRY_SIZE;
  1793.  
  1794.       /* We also need an entry in the .rela.plt section.  */
  1795.       s = bfd_get_section_by_name(dynobj, ".rela.plt");
  1796.       BFD_ASSERT(s != NULL);
  1797.       s->_raw_size += sizeof(Elf64_External_Rela);
  1798.  
  1799.       return true;
  1800.     }
  1801.  
  1802.   /* If this is a weak symbol, and there is a real definition, the
  1803.      processor independent code will have arranged for us to see the
  1804.      real definition first, and we can just use the same value.  */
  1805.   if (h->weakdef != NULL)
  1806.     {
  1807.       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
  1808.           || h->weakdef->root.type == bfd_link_hash_defweak);
  1809.       h->root.u.def.section = h->weakdef->root.u.def.section;
  1810.       h->root.u.def.value = h->weakdef->root.u.def.value;
  1811.       return true;
  1812.     }
  1813.  
  1814.   /* This is a reference to a symbol defined by a dynamic object which
  1815.      is not a function.  The Alpha, since it uses .got entries for
  1816.      symbols even in regular objects, does not need the hackery of a
  1817.      .dynbss section and COPY dynamic relocations.  */
  1818.  
  1819.   return true;
  1820. }
  1821.  
  1822. /* Set the sizes of the dynamic sections.  */
  1823.  
  1824. static boolean
  1825. elf64_alpha_size_dynamic_sections (output_bfd, info)
  1826.      bfd *output_bfd;
  1827.      struct bfd_link_info *info;
  1828. {
  1829.   bfd *dynobj;
  1830.   asection *s;
  1831.   boolean reltext;
  1832.   boolean relplt;
  1833.  
  1834.   dynobj = elf_hash_table(info)->dynobj;
  1835.   BFD_ASSERT(dynobj != NULL);
  1836.  
  1837.   if (elf_hash_table(info)->dynamic_sections_created)
  1838.     {
  1839.       /* Set the contents of the .interp section to the interpreter.  */
  1840.       if (!info->shared)
  1841.     {
  1842.       s = bfd_get_section_by_name(dynobj, ".interp");
  1843.       BFD_ASSERT(s != NULL);
  1844.       s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
  1845.       s->contents = (unsigned char *)ELF_DYNAMIC_INTERPRETER;
  1846.     }
  1847.     }
  1848.   else
  1849.     {
  1850.       /* We may have created entries in the .rela.got section.
  1851.          However, if we are not creating the dynamic sections, we will
  1852.          not actually use these entries.  Reset the size of .rel.got,
  1853.          which will cause it to get stripped from the output file
  1854.          below.  */
  1855.       s = bfd_get_section_by_name (dynobj, ".rela.got");
  1856.       if (s != NULL)
  1857.     s->_raw_size = 0;
  1858.     }
  1859.  
  1860.   /* The check_relocs and adjust_dynamic_symbol entry points have
  1861.      determined the sizes of the various dynamic sections.  Allocate
  1862.      memory for them.  */
  1863.   reltext = false;
  1864.   relplt = false;
  1865.   for (s = dynobj->sections; s != NULL; s = s->next)
  1866.     {
  1867.       const char *name;
  1868.       boolean strip;
  1869.  
  1870.       if (!(s->flags & SEC_IN_MEMORY))
  1871.     continue;
  1872.  
  1873.       /* It's OK to base decisions on the section name, because none
  1874.      of the dynobj section names depend upon the input files.  */
  1875.       name = bfd_get_section_name(dynobj, s);
  1876.  
  1877.       /* If we don't need this section, strip it from the output file.
  1878.      This is to handle .rela.bss and .rela.plt.  We must create it
  1879.      in create_dynamic_sections, because it must be created before
  1880.      the linker maps input sections to output sections.  The
  1881.      linker does that before adjust_dynamic_symbol is called, and
  1882.      it is that function which decides whether anything needs to
  1883.      go into these sections.  */
  1884.  
  1885.       strip = false;
  1886.  
  1887.       if (strncmp(name, ".rela", 5) == 0)
  1888.     {
  1889.       strip = (s->_raw_size == 0);
  1890.  
  1891.       if (!strip)
  1892.         {
  1893.           asection *target;
  1894.  
  1895.           /* If this relocation section applies to a read only
  1896.          section, then we probably need a DT_TEXTREL entry.  */
  1897.           target = bfd_get_section_by_name (output_bfd, name + 5);
  1898.           if (target != NULL
  1899.           && (target->flags & SEC_READONLY) != 0)
  1900.         reltext = true;
  1901.  
  1902.           if (strcmp(name, ".rela.plt") == 0)
  1903.         relplt = true;
  1904.  
  1905.           /* We use the reloc_count field as a counter if we need
  1906.          to copy relocs into the output file.  */
  1907.           s->reloc_count = 0;
  1908.         }
  1909.     }
  1910.       else if (strcmp(name, ".got") == 0)
  1911.     {
  1912.       /* If we are generating a shared library, we generate a
  1913.          section symbol for each output section.  These are local
  1914.          symbols, which means that they must come first in the
  1915.          dynamic symbol table.  That means we must increment the
  1916.          dynamic symbol index of every other dynamic symbol.  */
  1917.       if (info->shared)
  1918.         {
  1919.           long c[2], i;
  1920.           asection *p;
  1921.  
  1922.           c[0] = 0;
  1923.           c[1] = bfd_count_sections(output_bfd);
  1924.  
  1925.           elf_link_hash_traverse (elf_hash_table(info),
  1926.                       elf64_alpha_adjust_dynindx,
  1927.                       (PTR)c);
  1928.           elf_hash_table (info)->dynsymcount += c[1];
  1929.  
  1930.           for (i = 3, p = output_bfd->sections;
  1931.            p != NULL;
  1932.            p = p->next, i++)
  1933.         {
  1934.           elf_section_data (p)->dynindx = i;
  1935.           /* These symbols will have no names, so we don't need to
  1936.              fiddle with dynstr_index.  */
  1937.         }
  1938.         }
  1939.     }
  1940.       else if (strcmp (name, ".plt") != 0)
  1941.     {
  1942.       /* It's not one of our sections, so don't allocate space.  */
  1943.       continue;
  1944.     }
  1945.  
  1946.       if (strip)
  1947.     {
  1948.       asection **spp;
  1949.  
  1950.       for (spp = &s->output_section->owner->sections;
  1951.            *spp != s->output_section;
  1952.            spp = &(*spp)->next)
  1953.         continue;
  1954.       *spp = s->output_section->next;
  1955.       --s->output_section->owner->section_count;
  1956.  
  1957.       continue;
  1958.     }
  1959.  
  1960.       /* Allocate memory for the section contents.  */
  1961.       s->contents = (bfd_byte *) bfd_zalloc(dynobj, s->_raw_size);
  1962.       if (s->contents == NULL && s->_raw_size != 0)
  1963.     return false;
  1964.     }
  1965.  
  1966.   if (elf_hash_table (info)->dynamic_sections_created)
  1967.     {
  1968.       /* Add some entries to the .dynamic section.  We fill in the
  1969.      values later, in elf64_alpha_finish_dynamic_sections, but we
  1970.      must add the entries now so that we get the correct size for
  1971.      the .dynamic section.  The DT_DEBUG entry is filled in by the
  1972.      dynamic linker and used by the debugger.  */
  1973.       if (!info->shared)
  1974.     {
  1975.       if (!bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
  1976.         return false;
  1977.     }
  1978.  
  1979.       if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0))
  1980.     return false;
  1981.  
  1982.       if (relplt)
  1983.     {
  1984.       if (! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
  1985.           || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
  1986.           || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
  1987.         return false;
  1988.     }
  1989.  
  1990.       if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
  1991.       || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
  1992.       || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
  1993.                         sizeof(Elf64_External_Rela)))
  1994.     return false;
  1995.  
  1996.       if (reltext)
  1997.     {
  1998.       if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
  1999.         return false;
  2000.     }
  2001.     }
  2002.  
  2003.   return true;
  2004. }
  2005.  
  2006. /* Increment the index of a dynamic symbol by a given amount.  Called
  2007.    via elf_link_hash_traverse.  */
  2008.  
  2009. static boolean
  2010. elf64_alpha_adjust_dynindx (h, cparg)
  2011.      struct elf_link_hash_entry *h;
  2012.      PTR cparg;
  2013. {
  2014.   long *cp = (long *)cparg;
  2015.  
  2016.   if (h->dynindx >= cp[0])
  2017.     h->dynindx += cp[1];
  2018.  
  2019.   return true;
  2020. }
  2021.  
  2022. /* Relocate an Alpha ELF section.  */
  2023.  
  2024. static boolean
  2025. elf64_alpha_relocate_section (output_bfd, info, input_bfd, input_section,
  2026.                   contents, relocs, local_syms, local_sections)
  2027.      bfd *output_bfd;
  2028.      struct bfd_link_info *info;
  2029.      bfd *input_bfd;
  2030.      asection *input_section;
  2031.      bfd_byte *contents;
  2032.      Elf_Internal_Rela *relocs;
  2033.      Elf_Internal_Sym *local_syms;
  2034.      asection **local_sections;
  2035. {
  2036.   Elf_Internal_Shdr *symtab_hdr;
  2037.   Elf_Internal_Rela *rel;
  2038.   Elf_Internal_Rela *relend;
  2039.   asection *sec, *sgot, *splt;
  2040.   bfd *dynobj;
  2041.   bfd_vma gp;
  2042.  
  2043.   symtab_hdr = &elf_tdata(input_bfd)->symtab_hdr;
  2044.  
  2045.   /* Find the gp value for this input bfd.  */
  2046.   sgot = NULL;
  2047.   gp = 0;
  2048.   dynobj = elf_hash_table(info)->dynobj;
  2049.   if (dynobj)
  2050.     {
  2051.       sgot = bfd_get_section_by_name (dynobj, ".got");
  2052.       splt = bfd_get_section_by_name (dynobj, ".plt");
  2053.  
  2054.       gp = _bfd_get_gp_value(dynobj);
  2055.       if (gp == 0)
  2056.     {
  2057.       gp = (sgot->output_section->vma
  2058.         + sgot->output_offset
  2059.         + 0x8000);
  2060.       _bfd_set_gp_value(dynobj, gp);
  2061.     }
  2062.     }
  2063.  
  2064.   rel = relocs;
  2065.   relend = relocs + input_section->reloc_count;
  2066.   for (; rel < relend; rel++)
  2067.     {
  2068.       int r_type;
  2069.       reloc_howto_type *howto;
  2070.       unsigned long r_symndx;
  2071.       struct elf_link_hash_entry *h;
  2072.       Elf_Internal_Sym *sym;
  2073.       bfd_vma relocation;
  2074.       bfd_vma addend;
  2075.       bfd_reloc_status_type r;
  2076.  
  2077.       r_type = ELF64_R_TYPE(rel->r_info);
  2078.       if (r_type < 0 || r_type >= (int) R_ALPHA_max)
  2079.     {
  2080.       bfd_set_error (bfd_error_bad_value);
  2081.       return false;
  2082.     }
  2083.       howto = elf64_alpha_howto_table + r_type;
  2084.  
  2085.       r_symndx = ELF64_R_SYM(rel->r_info);
  2086.  
  2087.       if (info->relocateable)
  2088.     {
  2089.       /* This is a relocateable link.  We don't have to change
  2090.          anything, unless the reloc is against a section symbol,
  2091.          in which case we have to adjust according to where the
  2092.          section symbol winds up in the output section.  */
  2093.       if (r_symndx < symtab_hdr->sh_info)
  2094.         {
  2095.           sym = local_syms + r_symndx;
  2096.           if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
  2097.         {
  2098.           sec = local_sections[r_symndx];
  2099.           rel->r_addend += sec->output_offset + sym->st_value;
  2100.         }
  2101.         }
  2102.  
  2103.       continue;
  2104.     }
  2105.  
  2106.       /* This is a final link.  */
  2107.  
  2108.       h = NULL;
  2109.       sym = NULL;
  2110.       sec = NULL;
  2111.  
  2112.       if (r_symndx < symtab_hdr->sh_info)
  2113.     {
  2114.       sym = local_syms + r_symndx;
  2115.       sec = local_sections[r_symndx];
  2116.       relocation = (sec->output_section->vma
  2117.             + sec->output_offset
  2118.             + sym->st_value);
  2119.     }
  2120.       else
  2121.     {
  2122.       h = elf_sym_hashes(input_bfd)[r_symndx - symtab_hdr->sh_info];
  2123.  
  2124.       while (h->root.type == bfd_link_hash_indirect
  2125.          || h->root.type == bfd_link_hash_warning)
  2126.         h = (struct elf_link_hash_entry *)h->root.u.i.link;
  2127.  
  2128.       if (h->root.type == bfd_link_hash_defined
  2129.           || h->root.type == bfd_link_hash_defweak)
  2130.         {
  2131.           sec = h->root.u.def.section;
  2132.  
  2133.           /* If the symbol was defined as a common symbol in a
  2134.          regular object file, and there was no definition in
  2135.          any dynamic object, then the linker will have
  2136.          allocated space for the symbol in a common section
  2137.          but the ELF_LINK_HASH_DEF_REGULAR flag will not have
  2138.          been set.  This is done for dynamic symbols in
  2139.          elf_adjust_dynamic_symbol but this is not done for
  2140.          non-dynamic symbols, somehow.  */
  2141.           if ((h->elf_link_hash_flags
  2142.            & (ELF_LINK_HASH_DEF_REGULAR
  2143.               | ELF_LINK_HASH_REF_REGULAR
  2144.               | ELF_LINK_HASH_DEF_DYNAMIC))
  2145.           == ELF_LINK_HASH_REF_REGULAR
  2146.           && !(sec->owner->flags & DYNAMIC))
  2147.         h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  2148.  
  2149. #if rth_notdef
  2150.           if ((r_type == R_ALPHA_LITERAL
  2151.            && elf_hash_table(info)->dynamic_sections_created
  2152.            && (!info->shared
  2153.                || !info->symbolic
  2154.                || !(h->elf_link_hash_flags
  2155.                 & ELF_LINK_HASH_DEF_REGULAR)))
  2156.           || (info->shared
  2157.               && (!info->symbolic
  2158.               || !(h->elf_link_hash_flags
  2159.                    & ELF_LINK_HASH_DEF_REGULAR))
  2160.               && (input_section->flags & SEC_ALLOC)
  2161.               && (r_type == R_ALPHA_REFLONG
  2162.               || r_type == R_ALPHA_REFQUAD
  2163.               || r_type == R_ALPHA_LITERAL)))
  2164.         {
  2165.           /* In these cases, we don't need the relocation value.
  2166.              We check specially because in some obscure cases
  2167.              sec->output_section will be NULL.  */
  2168.           relocation = 0;
  2169.         }
  2170. #else
  2171.           /* FIXME: Are not these obscure cases simply bugs?  Let's
  2172.          get something working and come back to this.  */
  2173.           if (sec->output_section == NULL)
  2174.         relocation = 0;
  2175. #endif /* rth_notdef */
  2176.           else
  2177.         {
  2178.           relocation = (h->root.u.def.value
  2179.                 + sec->output_section->vma
  2180.                 + sec->output_offset);
  2181.         }
  2182.         }
  2183.       else if (h->root.type == bfd_link_hash_undefweak)
  2184.         relocation = 0;
  2185.       else if (info->shared && !info->symbolic)
  2186.         relocation = 0;
  2187.       else
  2188.         {
  2189.           if (!((*info->callbacks->undefined_symbol)
  2190.             (info, h->root.root.string, input_bfd,
  2191.              input_section, rel->r_offset)))
  2192.         return false;
  2193.           relocation = 0;
  2194.         }
  2195.     }
  2196.       addend = rel->r_addend;
  2197.  
  2198.       switch (r_type)
  2199.     {
  2200.     case R_ALPHA_GPDISP:
  2201.       {
  2202.         bfd_byte *p_ldah, *p_lda;
  2203.  
  2204.         relocation = (input_section->output_section->vma
  2205.               + input_section->output_offset
  2206.               + rel->r_offset);
  2207.  
  2208.         p_ldah = contents + rel->r_offset - input_section->vma;
  2209.         p_lda = p_ldah + rel->r_addend;
  2210.  
  2211.         r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - relocation,
  2212.                          p_ldah, p_lda);
  2213.       }
  2214.       break;
  2215.  
  2216.     case R_ALPHA_OP_PUSH:
  2217.     case R_ALPHA_OP_STORE:
  2218.     case R_ALPHA_OP_PSUB:
  2219.     case R_ALPHA_OP_PRSHIFT:
  2220.       /* FIXME */
  2221.       abort();
  2222.  
  2223.     case R_ALPHA_LITERAL:
  2224.       {
  2225.         bfd_vma gotoff;
  2226.  
  2227.         BFD_ASSERT(gp != 0);
  2228.         BFD_ASSERT(sgot != NULL);
  2229.         if (h != NULL)
  2230.           {
  2231.         gotoff = h->got_offset;
  2232.           }
  2233.         else
  2234.           {
  2235.         gotoff = elf_local_got_offsets (input_bfd)[r_symndx];
  2236.  
  2237.         /* Use the lsb as a flag indicating that we've already
  2238.            output the relocation entry.  */
  2239.         if (info->shared)
  2240.           if (gotoff & 1)
  2241.             gotoff &= ~(bfd_vma)1;
  2242.           else
  2243.             {
  2244.               asection *srel;
  2245.               Elf_Internal_Rela outrel;
  2246.  
  2247.               srel = bfd_get_section_by_name (dynobj, ".rela.got");
  2248.               BFD_ASSERT(srel != NULL);
  2249.  
  2250.               outrel.r_offset = (sgot->output_section->vma
  2251.                      + sgot->output_offset + gotoff);
  2252.               outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
  2253.               outrel.r_addend = 0;
  2254.  
  2255.               bfd_elf64_swap_reloca_out (output_bfd, &outrel,
  2256.                          ((Elf64_External_Rela *)
  2257.                           srel->contents)
  2258.                          + srel->reloc_count++);
  2259.  
  2260.               elf_local_got_offsets (input_bfd)[r_symndx] |= 1;
  2261.             }
  2262.           }
  2263.  
  2264.         /* Initialize the .got entry.  */
  2265.         bfd_put_64 (output_bfd, relocation, sgot->contents + gotoff);
  2266.  
  2267.         /* Figure the gprel relocation.  */
  2268.         addend = 0;
  2269.         relocation = (sgot->output_section->vma
  2270.               + sgot->output_offset
  2271.               + gotoff);
  2272.         relocation -= gp;
  2273.       }
  2274.       /* overflow handled by _bfd_final_link_relocate */
  2275.       goto default_reloc;
  2276.       
  2277.     case R_ALPHA_GPREL32:
  2278.       BFD_ASSERT(gp != 0);
  2279.       relocation -= gp;
  2280.       goto default_reloc;
  2281.  
  2282.     case R_ALPHA_BRADDR:
  2283.     case R_ALPHA_HINT:
  2284.       /* The regular PC-relative stuff measures from the start of
  2285.          the instruction rather than the end.  */
  2286.       addend -= 4;
  2287.       goto default_reloc;
  2288.       
  2289.     case R_ALPHA_REFLONG:
  2290.     case R_ALPHA_REFQUAD:
  2291.       if (info->shared
  2292.           || (h && !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
  2293.         {
  2294.           asection *srel;
  2295.           const char *name;
  2296.           Elf_Internal_Rela outrel;
  2297.  
  2298.           name = (bfd_elf_string_from_elf_section
  2299.               (input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
  2300.                elf_section_data(input_section)->rel_hdr.sh_name));
  2301.           BFD_ASSERT(name != NULL);
  2302.  
  2303.           srel = bfd_get_section_by_name(dynobj, name);
  2304.           BFD_ASSERT(srel != NULL);
  2305.  
  2306.           outrel.r_offset = (input_section->output_section->vma
  2307.                  + input_section->output_offset
  2308.                  + rel->r_offset);
  2309.           outrel.r_addend = 0;
  2310.           if (h)
  2311.         {
  2312.           BFD_ASSERT(h->dynindx != -1);
  2313.           outrel.r_info = ELF64_R_INFO(h->dynindx, r_type);
  2314.           relocation = 0;
  2315.         }
  2316.           else
  2317.         {
  2318.           outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
  2319.         }
  2320.  
  2321.           bfd_elf64_swap_reloca_out (output_bfd, &outrel,
  2322.                      ((Elf64_External_Rela *)
  2323.                       srel->contents)
  2324.                      + srel->reloc_count++);
  2325.         }
  2326.       goto default_reloc;
  2327.  
  2328.     default:
  2329.     default_reloc:
  2330.       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  2331.                     contents, rel->r_offset, relocation,
  2332.                     addend);
  2333.       break;
  2334.     }
  2335.  
  2336.       switch (r)
  2337.     {
  2338.     case bfd_reloc_ok:
  2339.       break;
  2340.  
  2341.     case bfd_reloc_overflow:
  2342.       {
  2343.         const char *name;
  2344.  
  2345.         if (h != NULL)
  2346.           name = h->root.root.string;
  2347.         else
  2348.           {
  2349.         name = (bfd_elf_string_from_elf_section
  2350.             (input_bfd, symtab_hdr->sh_link, sym->st_name));
  2351.         if (name == NULL)
  2352.           return false;
  2353.         if (*name == '\0')
  2354.           name = bfd_section_name (input_bfd, sec);
  2355.           }
  2356.         if (! ((*info->callbacks->reloc_overflow)
  2357.            (info, name, howto->name, (bfd_vma) 0,
  2358.             input_bfd, input_section, rel->r_offset)))
  2359.           return false;
  2360.       }
  2361.       break;
  2362.  
  2363.     default:
  2364.     case bfd_reloc_outofrange:
  2365.       abort ();
  2366.     }
  2367.     }
  2368.  
  2369.   return true;
  2370. }
  2371.  
  2372. /* Finish up dynamic symbol handling.  We set the contents of various
  2373.    dynamic sections here.  */
  2374.  
  2375. static boolean
  2376. elf64_alpha_finish_dynamic_symbol (output_bfd, info, h, sym)
  2377.      bfd *output_bfd;
  2378.      struct bfd_link_info *info;
  2379.      struct elf_link_hash_entry *h;
  2380.      Elf_Internal_Sym *sym;
  2381. {
  2382.   bfd *dynobj = elf_hash_table(info)->dynobj;
  2383.  
  2384.   if (h->plt_offset != MINUS_ONE)
  2385.     {
  2386.       asection *splt, *sgot, *srel;
  2387.       Elf_Internal_Rela outrel;
  2388.       bfd_vma got_addr, plt_addr;
  2389.       bfd_vma plt_index;
  2390.  
  2391.       /* This symbol has an entry in the procedure linkage table.  */
  2392.  
  2393.       BFD_ASSERT(h->dynindx != -1);
  2394.       BFD_ASSERT(h->got_offset != MINUS_ONE);
  2395.  
  2396.       splt = bfd_get_section_by_name(dynobj, ".plt");
  2397.       BFD_ASSERT(splt != NULL);
  2398.       srel = bfd_get_section_by_name(dynobj, ".rela.plt");
  2399.       BFD_ASSERT(srel != NULL);
  2400.       sgot = bfd_get_section_by_name(dynobj, ".got");
  2401.       BFD_ASSERT(sgot != NULL);
  2402.  
  2403.       got_addr = (sgot->output_section->vma
  2404.           + sgot->output_offset 
  2405.           + h->got_offset);
  2406.       plt_addr = (splt->output_section->vma
  2407.           + splt->output_offset
  2408.           + h->plt_offset);
  2409.  
  2410.       plt_index = (h->plt_offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
  2411.  
  2412.       /* Fill in the entry in the procedure linkage table.  */
  2413.       {
  2414.     unsigned insn1, insn2, insn3;
  2415.     long hi, lo;
  2416.  
  2417.     /* decompose the reloc offset for the plt for ldah+lda */
  2418.     hi = plt_index * sizeof(Elf64_External_Rela);
  2419.     lo = ((hi & 0xffff) ^ 0x8000) - 0x8000;
  2420.     hi = (hi - lo) >> 16;
  2421.  
  2422.     insn1 = PLT_ENTRY_WORD1 | (hi & 0xffff);
  2423.     insn2 = PLT_ENTRY_WORD2 | (lo & 0xffff);
  2424.     insn3 = PLT_ENTRY_WORD3 | ((-(h->plt_offset + 12) >> 2) & 0x1fffff);
  2425.     
  2426.     bfd_put_32 (output_bfd, insn1, splt->contents + h->plt_offset);
  2427.     bfd_put_32 (output_bfd, insn2, splt->contents + h->plt_offset + 4);
  2428.     bfd_put_32 (output_bfd, insn3, splt->contents + h->plt_offset + 8);
  2429.       }
  2430.  
  2431.       /* Fill in the entry in the .rela.plt section.  */
  2432.       outrel.r_offset = got_addr;
  2433.       outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
  2434.       outrel.r_addend = 0;
  2435.  
  2436.       bfd_elf64_swap_reloca_out (output_bfd, &outrel,
  2437.                  ((Elf64_External_Rela *)srel->contents
  2438.                   + plt_index));
  2439.  
  2440.       if (!(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
  2441.     {
  2442.       /* Mark the symbol as undefined, rather than as defined in the
  2443.          .plt section.  Leave the value alone.  */
  2444.       sym->st_shndx = SHN_UNDEF;
  2445.     }
  2446.  
  2447.       /* Fill in the entry in the global offset table.  */
  2448.       bfd_put_64 (output_bfd, plt_addr, sgot->contents + h->got_offset);
  2449.     }
  2450.   else if (h->got_offset != MINUS_ONE)
  2451.     {
  2452.       asection *sgot, *srel;
  2453.       Elf_Internal_Rela outrel;
  2454.  
  2455.       BFD_ASSERT(h->dynindx != -1);
  2456.  
  2457.       sgot = bfd_get_section_by_name (dynobj, ".got");
  2458.       BFD_ASSERT (sgot != NULL);
  2459.       srel = bfd_get_section_by_name (dynobj, ".rela.got");
  2460.       BFD_ASSERT (srel != NULL);
  2461.  
  2462.       outrel.r_offset = (sgot->output_section->vma
  2463.                + sgot->output_offset
  2464.                + h->got_offset);
  2465.       outrel.r_addend = 0;
  2466.       if (info->shared
  2467.       && info->symbolic
  2468.       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
  2469.     outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
  2470.       else
  2471.     {
  2472.       bfd_put_64(output_bfd, (bfd_vma)0, sgot->contents + h->got_offset);
  2473.       outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_GLOB_DAT);
  2474.     }
  2475.  
  2476.       bfd_elf64_swap_reloca_out (output_bfd, &outrel,
  2477.                  ((Elf64_External_Rela *)srel->contents
  2478.                   + srel->reloc_count++));
  2479.     }
  2480.  
  2481.   /* Mark some specially defined symbols as absolute.  */
  2482.   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
  2483.       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
  2484.       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
  2485.     sym->st_shndx = SHN_ABS;
  2486.  
  2487.   return true;
  2488. }
  2489.  
  2490. /* Finish up the dynamic sections.  */
  2491.  
  2492. static boolean
  2493. elf64_alpha_finish_dynamic_sections (output_bfd, info)
  2494.      bfd *output_bfd;
  2495.      struct bfd_link_info *info;
  2496. {
  2497.   bfd *dynobj;
  2498.   asection *sdyn;
  2499.   asection *sgot;
  2500.  
  2501.   dynobj = elf_hash_table (info)->dynobj;
  2502.   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
  2503.  
  2504.   if (elf_hash_table (info)->dynamic_sections_created)
  2505.     {
  2506.       asection *splt;
  2507.       Elf64_External_Dyn *dyncon, *dynconend;
  2508.  
  2509.       splt = bfd_get_section_by_name (dynobj, ".plt");
  2510.       BFD_ASSERT (splt != NULL && sdyn != NULL);
  2511.  
  2512.       dyncon = (Elf64_External_Dyn *) sdyn->contents;
  2513.       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
  2514.       for (; dyncon < dynconend; dyncon++)
  2515.     {
  2516.       Elf_Internal_Dyn dyn;
  2517.       const char *name;
  2518.       asection *s;
  2519.  
  2520.       bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
  2521.  
  2522.       switch (dyn.d_tag)
  2523.         {
  2524.         case DT_PLTGOT:
  2525.           name = ".plt";
  2526.           goto get_vma;
  2527.         case DT_PLTRELSZ:
  2528.           name = ".rela.plt";
  2529.           goto get_size;
  2530.         case DT_JMPREL:
  2531.           name = ".rela.plt";
  2532.           goto get_vma;
  2533.  
  2534.         case DT_RELASZ:
  2535.           /* My interpretation of the TIS v1.1 ELF document indicates
  2536.          that RELASZ should not include JMPREL.  This is not what
  2537.          the rest of the BFD does.  It is, however, what the 
  2538.          glibc ld.so wants.  Do this fixup here until we found 
  2539.          out who is right.  */
  2540.           s = bfd_get_section_by_name (output_bfd, ".rela.plt");
  2541.           if (s)
  2542.         {
  2543.           dyn.d_un.d_val -=
  2544.             (s->_cooked_size ? s->_cooked_size : s->_raw_size);
  2545.         }
  2546.           break;
  2547.  
  2548.         get_vma:
  2549.           s = bfd_get_section_by_name (output_bfd, name);
  2550.           dyn.d_un.d_ptr = (s ? s->vma : 0);
  2551.           break;
  2552.  
  2553.         get_size:
  2554.           s = bfd_get_section_by_name (output_bfd, name);
  2555.           dyn.d_un.d_val =
  2556.         (s->_cooked_size ? s->_cooked_size : s->_raw_size);
  2557.           break;
  2558.         }
  2559.  
  2560.       bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
  2561.     }
  2562.  
  2563.       /* Initialize the PLT0 entry */
  2564.       if (splt->_raw_size > 0)
  2565.     {
  2566.       bfd_put_32 (output_bfd, PLT_HEADER_WORD1, splt->contents);
  2567.       bfd_put_32 (output_bfd, PLT_HEADER_WORD2, splt->contents + 4);
  2568.       bfd_put_32 (output_bfd, PLT_HEADER_WORD3, splt->contents + 8);
  2569.       bfd_put_32 (output_bfd, PLT_HEADER_WORD4, splt->contents + 12);
  2570.       
  2571.       /* The next two words will be filled in by ld.so */
  2572.       bfd_put_64 (output_bfd, 0, splt->contents + 16);
  2573.       bfd_put_64 (output_bfd, 0, splt->contents + 24);
  2574.  
  2575.       elf_section_data (splt->output_section)->this_hdr.sh_entsize = 
  2576.         PLT_HEADER_SIZE;
  2577.     }
  2578.     }
  2579.  
  2580.   /* Set the first entry in the global offset table to the address of
  2581.      the dynamic section.  */
  2582.   sgot = bfd_get_section_by_name (dynobj, ".got");
  2583.   if (sgot && sgot->_raw_size > 0)
  2584.     {
  2585.       if (sdyn == NULL)
  2586.         bfd_put_64 (output_bfd, (bfd_vma)0, sgot->contents);
  2587.       else
  2588.         bfd_put_64 (output_bfd,
  2589.             sdyn->output_section->vma + sdyn->output_offset,
  2590.             sgot->contents);
  2591.  
  2592.       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 
  2593.     8 * RESERVED_GOT_ENTRIES;
  2594.     }
  2595.  
  2596.   if (info->shared)
  2597.     {
  2598.       asection *sdynsym;
  2599.       asection *s;
  2600.       Elf_Internal_Sym sym;
  2601.  
  2602.       /* Set up the section symbols for the output sections.  */
  2603.  
  2604.       sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
  2605.       BFD_ASSERT (sdynsym != NULL);
  2606.  
  2607.       sym.st_size = 0;
  2608.       sym.st_name = 0;
  2609.       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  2610.       sym.st_other = 0;
  2611.  
  2612.       for (s = output_bfd->sections; s != NULL; s = s->next)
  2613.     {
  2614.       int indx;
  2615.  
  2616.       sym.st_value = s->vma;
  2617.  
  2618.       indx = elf_section_data (s)->this_idx;
  2619.       BFD_ASSERT (indx > 0);
  2620.       sym.st_shndx = indx;
  2621.  
  2622.       bfd_elf64_swap_symbol_out (output_bfd, &sym,
  2623.                      (PTR) (((Elf64_External_Sym *)
  2624.                          sdynsym->contents)
  2625.                         + elf_section_data (s)->dynindx));
  2626.     }
  2627.  
  2628.       /* Set the sh_info field of the output .dynsym section to the
  2629.          index of the first global symbol.  */
  2630.       elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
  2631.     bfd_count_sections (output_bfd) + 1;
  2632.     }
  2633.  
  2634.   return true;
  2635. }
  2636.  
  2637. /* We need to use a special link routine to handle the .reginfo and
  2638.    the .mdebug sections.  We need to merge all instances of these
  2639.    sections together, not write them all out sequentially.  */
  2640.  
  2641. static boolean
  2642. elf64_alpha_final_link (abfd, info)
  2643.      bfd *abfd;
  2644.      struct bfd_link_info *info;
  2645. {
  2646.   asection *o;
  2647.   struct bfd_link_order *p;
  2648.   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
  2649.   struct ecoff_debug_info debug;
  2650.   const struct ecoff_debug_swap *swap
  2651.     = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  2652.   HDRR *symhdr = &debug.symbolic_header;
  2653.   PTR mdebug_handle = NULL;
  2654.  
  2655.   /* Go through the sections and collect the .reginfo and .mdebug
  2656.      information.  */
  2657.   reginfo_sec = NULL;
  2658.   mdebug_sec = NULL;
  2659.   gptab_data_sec = NULL;
  2660.   gptab_bss_sec = NULL;
  2661.   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  2662.     {
  2663. #ifdef ERIC_neverdef
  2664.       if (strcmp (o->name, ".reginfo") == 0)
  2665.     {
  2666.       memset (®info, 0, sizeof reginfo);
  2667.  
  2668.       /* We have found the .reginfo section in the output file.
  2669.          Look through all the link_orders comprising it and merge
  2670.          the information together.  */
  2671.       for (p = o->link_order_head;
  2672.            p != (struct bfd_link_order *) NULL;
  2673.            p = p->next)
  2674.         {
  2675.           asection *input_section;
  2676.           bfd *input_bfd;
  2677.           Elf64_External_RegInfo ext;
  2678.           Elf64_RegInfo sub;
  2679.  
  2680.           if (p->type != bfd_indirect_link_order)
  2681.         {
  2682.           if (p->type == bfd_fill_link_order)
  2683.             continue;
  2684.           abort ();
  2685.         }
  2686.  
  2687.           input_section = p->u.indirect.section;
  2688.           input_bfd = input_section->owner;
  2689.  
  2690.           /* The linker emulation code has probably clobbered the
  2691.                  size to be zero bytes.  */
  2692.           if (input_section->_raw_size == 0)
  2693.         input_section->_raw_size = sizeof (Elf64_External_RegInfo);
  2694.  
  2695.           if (! bfd_get_section_contents (input_bfd, input_section,
  2696.                           (PTR) &ext,
  2697.                           (file_ptr) 0,
  2698.                           sizeof ext))
  2699.         return false;
  2700.  
  2701.           bfd_alpha_elf64_swap_reginfo_in (input_bfd, &ext, &sub);
  2702.  
  2703.           reginfo.ri_gprmask |= sub.ri_gprmask;
  2704.           reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
  2705.           reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
  2706.           reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
  2707.           reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
  2708.  
  2709.           /* ri_gp_value is set by the function
  2710.          alpha_elf_section_processing when the section is
  2711.          finally written out.  */
  2712.  
  2713.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  2714.          elf_link_input_bfd ignores this section.  */
  2715.           input_section->flags &=~ SEC_HAS_CONTENTS;
  2716.         }
  2717.  
  2718.       /* Force the section size to the value we want.  */
  2719.       o->_raw_size = sizeof (Elf64_External_RegInfo);
  2720.  
  2721.       /* Skip this section later on (I don't think this currently
  2722.          matters, but someday it might).  */
  2723.       o->link_order_head = (struct bfd_link_order *) NULL;
  2724.  
  2725.       reginfo_sec = o;
  2726.     }
  2727. #endif
  2728.  
  2729.       if (strcmp (o->name, ".mdebug") == 0)
  2730.     {
  2731.       struct extsym_info einfo;
  2732.  
  2733.       /* We have found the .mdebug section in the output file.
  2734.          Look through all the link_orders comprising it and merge
  2735.          the information together.  */
  2736.       symhdr->magic = swap->sym_magic;
  2737.       /* FIXME: What should the version stamp be?  */
  2738.       symhdr->vstamp = 0;
  2739.       symhdr->ilineMax = 0;
  2740.       symhdr->cbLine = 0;
  2741.       symhdr->idnMax = 0;
  2742.       symhdr->ipdMax = 0;
  2743.       symhdr->isymMax = 0;
  2744.       symhdr->ioptMax = 0;
  2745.       symhdr->iauxMax = 0;
  2746.       symhdr->issMax = 0;
  2747.       symhdr->issExtMax = 0;
  2748.       symhdr->ifdMax = 0;
  2749.       symhdr->crfd = 0;
  2750.       symhdr->iextMax = 0;
  2751.  
  2752.       /* We accumulate the debugging information itself in the
  2753.          debug_info structure.  */
  2754.       debug.line = NULL;
  2755.       debug.external_dnr = NULL;
  2756.       debug.external_pdr = NULL;
  2757.       debug.external_sym = NULL;
  2758.       debug.external_opt = NULL;
  2759.       debug.external_aux = NULL;
  2760.       debug.ss = NULL;
  2761.       debug.ssext = debug.ssext_end = NULL;
  2762.       debug.external_fdr = NULL;
  2763.       debug.external_rfd = NULL;
  2764.       debug.external_ext = debug.external_ext_end = NULL;
  2765.  
  2766.       mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
  2767.       if (mdebug_handle == (PTR) NULL)
  2768.         return false;
  2769.  
  2770.       if (1)
  2771.         {
  2772.           asection *s;
  2773.           EXTR esym;
  2774.           bfd_vma last;
  2775.           unsigned int i;
  2776.           static const char * const name[] =
  2777.         {
  2778.           ".text", ".init", ".fini", ".data",
  2779.           ".rodata", ".sdata", ".sbss", ".bss"
  2780.         };
  2781.           static const int sc[] = { scText, scInit, scFini, scData,
  2782.                       scRData, scSData, scSBss, scBss };
  2783.  
  2784.           esym.jmptbl = 0;
  2785.           esym.cobol_main = 0;
  2786.           esym.weakext = 0;
  2787.           esym.reserved = 0;
  2788.           esym.ifd = ifdNil;
  2789.           esym.asym.iss = issNil;
  2790.           esym.asym.st = stLocal;
  2791.           esym.asym.reserved = 0;
  2792.           esym.asym.index = indexNil;
  2793.           for (i = 0; i < 8; i++)
  2794.         {
  2795.           esym.asym.sc = sc[i];
  2796.           s = bfd_get_section_by_name (abfd, name[i]);
  2797.           if (s != NULL)
  2798.             {
  2799.               esym.asym.value = s->vma;
  2800.               last = s->vma + s->_raw_size;
  2801.             }
  2802.           else
  2803.             esym.asym.value = last;
  2804.         
  2805.           if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
  2806.                               name[i], &esym))
  2807.             return false;
  2808.         }
  2809.         }
  2810.  
  2811.       for (p = o->link_order_head;
  2812.            p != (struct bfd_link_order *) NULL;
  2813.            p = p->next)
  2814.         {
  2815.           asection *input_section;
  2816.           bfd *input_bfd;
  2817.           const struct ecoff_debug_swap *input_swap;
  2818.           struct ecoff_debug_info input_debug;
  2819.           char *eraw_src;
  2820.           char *eraw_end;
  2821.  
  2822.           if (p->type != bfd_indirect_link_order)
  2823.         {
  2824.           if (p->type == bfd_fill_link_order)
  2825.             continue;
  2826.           abort ();
  2827.         }
  2828.  
  2829.           input_section = p->u.indirect.section;
  2830.           input_bfd = input_section->owner;
  2831.  
  2832.           if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
  2833.           || (get_elf_backend_data (input_bfd)
  2834.               ->elf_backend_ecoff_debug_swap) == NULL)
  2835.         {
  2836.           /* I don't know what a non ALPHA ELF bfd would be
  2837.              doing with a .mdebug section, but I don't really
  2838.              want to deal with it.  */
  2839.           continue;
  2840.         }
  2841.  
  2842.           input_swap = (get_elf_backend_data (input_bfd)
  2843.                 ->elf_backend_ecoff_debug_swap);
  2844.  
  2845.           BFD_ASSERT (p->size == input_section->_raw_size);
  2846.  
  2847.           /* The ECOFF linking code expects that we have already
  2848.          read in the debugging information and set up an
  2849.          ecoff_debug_info structure, so we do that now.  */
  2850.           if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
  2851.                         &input_debug))
  2852.         return false;
  2853.  
  2854.           if (! (bfd_ecoff_debug_accumulate
  2855.              (mdebug_handle, abfd, &debug, swap, input_bfd,
  2856.               &input_debug, input_swap, info)))
  2857.         return false;
  2858.  
  2859.           /* Loop through the external symbols.  For each one with
  2860.          interesting information, try to find the symbol in
  2861.          the linker global hash table and save the information
  2862.          for the output external symbols.  */
  2863.           eraw_src = input_debug.external_ext;
  2864.           eraw_end = (eraw_src
  2865.               + (input_debug.symbolic_header.iextMax
  2866.                  * input_swap->external_ext_size));
  2867.           for (;
  2868.            eraw_src < eraw_end;
  2869.            eraw_src += input_swap->external_ext_size)
  2870.         {
  2871.           EXTR ext;
  2872.           const char *name;
  2873.           struct alpha_elf_link_hash_entry *h;
  2874.  
  2875.           (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
  2876.           if (ext.asym.sc == scNil
  2877.               || ext.asym.sc == scUndefined
  2878.               || ext.asym.sc == scSUndefined)
  2879.             continue;
  2880.  
  2881.           name = input_debug.ssext + ext.asym.iss;
  2882.           h = alpha_elf_link_hash_lookup (alpha_elf_hash_table (info),
  2883.                           name, false, false, true);
  2884.           if (h == NULL || h->esym.ifd != -2)
  2885.             continue;
  2886.  
  2887.           if (ext.ifd != -1)
  2888.             {
  2889.               BFD_ASSERT (ext.ifd
  2890.                   < input_debug.symbolic_header.ifdMax);
  2891.               ext.ifd = input_debug.ifdmap[ext.ifd];
  2892.             }
  2893.  
  2894.           h->esym = ext;
  2895.         }
  2896.  
  2897.           /* Free up the information we just read.  */
  2898.           free (input_debug.line);
  2899.           free (input_debug.external_dnr);
  2900.           free (input_debug.external_pdr);
  2901.           free (input_debug.external_sym);
  2902.           free (input_debug.external_opt);
  2903.           free (input_debug.external_aux);
  2904.           free (input_debug.ss);
  2905.           free (input_debug.ssext);
  2906.           free (input_debug.external_fdr);
  2907.           free (input_debug.external_rfd);
  2908.           free (input_debug.external_ext);
  2909.  
  2910.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  2911.          elf_link_input_bfd ignores this section.  */
  2912.           input_section->flags &=~ SEC_HAS_CONTENTS;
  2913.         }
  2914.  
  2915. #ifdef ERIC_neverdef
  2916.       if (info->shared)
  2917.         {
  2918.           /* Create .rtproc section.  */
  2919.           rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
  2920.           if (rtproc_sec == NULL)
  2921.         {
  2922.           flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
  2923.                     | SEC_READONLY);
  2924.  
  2925.           rtproc_sec = bfd_make_section (abfd, ".rtproc");
  2926.           if (rtproc_sec == NULL
  2927.               || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
  2928.               || ! bfd_set_section_alignment (abfd, rtproc_sec, 12))
  2929.             return false;
  2930.         }
  2931.  
  2932.           if (! alpha_elf_create_procedure_table (mdebug_handle, abfd,
  2933.                              info, rtproc_sec, &debug))
  2934.         return false;
  2935.         }
  2936. #endif
  2937.  
  2938.  
  2939.       /* Build the external symbol information.  */
  2940.       einfo.abfd = abfd;
  2941.       einfo.info = info;
  2942.       einfo.debug = &debug;
  2943.       einfo.swap = swap;
  2944.       einfo.failed = false;
  2945.       elf_link_hash_traverse (elf_hash_table (info),
  2946.                   elf64_alpha_output_extsym,
  2947.                   (PTR) &einfo);
  2948.       if (einfo.failed)
  2949.         return false;
  2950.  
  2951.       /* Set the size of the .mdebug section.  */
  2952.       o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
  2953.  
  2954.       /* Skip this section later on (I don't think this currently
  2955.          matters, but someday it might).  */
  2956.       o->link_order_head = (struct bfd_link_order *) NULL;
  2957.  
  2958.       mdebug_sec = o;
  2959.     }
  2960.  
  2961. #ifdef ERIC_neverdef
  2962.       if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
  2963.     {
  2964.       const char *subname;
  2965.       unsigned int c;
  2966.       Elf64_gptab *tab;
  2967.       Elf64_External_gptab *ext_tab;
  2968.       unsigned int i;
  2969.  
  2970.       /* The .gptab.sdata and .gptab.sbss sections hold
  2971.          information describing how the small data area would
  2972.          change depending upon the -G switch.  These sections
  2973.          not used in executables files.  */
  2974.       if (! info->relocateable)
  2975.         {
  2976.           asection **secpp;
  2977.  
  2978.           for (p = o->link_order_head;
  2979.            p != (struct bfd_link_order *) NULL;
  2980.            p = p->next)
  2981.         {
  2982.           asection *input_section;
  2983.  
  2984.           if (p->type != bfd_indirect_link_order)
  2985.             {
  2986.               if (p->type == bfd_fill_link_order)
  2987.             continue;
  2988.               abort ();
  2989.             }
  2990.  
  2991.           input_section = p->u.indirect.section;
  2992.  
  2993.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  2994.              elf_link_input_bfd ignores this section.  */
  2995.           input_section->flags &=~ SEC_HAS_CONTENTS;
  2996.         }
  2997.  
  2998.           /* Skip this section later on (I don't think this
  2999.          currently matters, but someday it might).  */
  3000.           o->link_order_head = (struct bfd_link_order *) NULL;
  3001.  
  3002.           /* Really remove the section.  */
  3003.           for (secpp = &abfd->sections;
  3004.            *secpp != o;
  3005.            secpp = &(*secpp)->next)
  3006.         ;
  3007.           *secpp = (*secpp)->next;
  3008.           --abfd->section_count;
  3009.  
  3010.           continue;
  3011.         }
  3012.  
  3013.       /* There is one gptab for initialized data, and one for
  3014.          uninitialized data.  */
  3015.       if (strcmp (o->name, ".gptab.sdata") == 0)
  3016.         gptab_data_sec = o;
  3017.       else if (strcmp (o->name, ".gptab.sbss") == 0)
  3018.         gptab_bss_sec = o;
  3019.       else
  3020.         {
  3021.           (*_bfd_error_handler)
  3022.         ("%s: illegal section name `%s'",
  3023.          bfd_get_filename (abfd), o->name);
  3024.           bfd_set_error (bfd_error_nonrepresentable_section);
  3025.           return false;
  3026.         }
  3027.  
  3028.       /* The linker script always combines .gptab.data and
  3029.          .gptab.sdata into .gptab.sdata, and likewise for
  3030.          .gptab.bss and .gptab.sbss.  It is possible that there is
  3031.          no .sdata or .sbss section in the output file, in which
  3032.          case we must change the name of the output section.  */
  3033.       subname = o->name + sizeof ".gptab" - 1;
  3034.       if (bfd_get_section_by_name (abfd, subname) == NULL)
  3035.         {
  3036.           if (o == gptab_data_sec)
  3037.         o->name = ".gptab.data";
  3038.           else
  3039.         o->name = ".gptab.bss";
  3040.           subname = o->name + sizeof ".gptab" - 1;
  3041.           BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
  3042.         }
  3043.  
  3044.       /* Set up the first entry.  */
  3045.       c = 1;
  3046.       tab = (Elf64_gptab *) bfd_malloc (c * sizeof (Elf64_gptab));
  3047.       if (tab == NULL)
  3048.         return false;
  3049.       tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
  3050.       tab[0].gt_header.gt_unused = 0;
  3051.  
  3052.       /* Combine the input sections.  */
  3053.       for (p = o->link_order_head;
  3054.            p != (struct bfd_link_order *) NULL;
  3055.            p = p->next)
  3056.         {
  3057.           asection *input_section;
  3058.           bfd *input_bfd;
  3059.           bfd_size_type size;
  3060.           unsigned long last;
  3061.           bfd_size_type gpentry;
  3062.  
  3063.           if (p->type != bfd_indirect_link_order)
  3064.         {
  3065.           if (p->type == bfd_fill_link_order)
  3066.             continue;
  3067.           abort ();
  3068.         }
  3069.  
  3070.           input_section = p->u.indirect.section;
  3071.           input_bfd = input_section->owner;
  3072.  
  3073.           /* Combine the gptab entries for this input section one
  3074.          by one.  We know that the input gptab entries are
  3075.          sorted by ascending -G value.  */
  3076.           size = bfd_section_size (input_bfd, input_section);
  3077.           last = 0;
  3078.           for (gpentry = sizeof (Elf64_External_gptab);
  3079.            gpentry < size;
  3080.            gpentry += sizeof (Elf64_External_gptab))
  3081.         {
  3082.           Elf64_External_gptab ext_gptab;
  3083.           Elf64_gptab int_gptab;
  3084.           unsigned long val;
  3085.           unsigned long add;
  3086.           boolean exact;
  3087.           unsigned int look;
  3088.  
  3089.           if (! (bfd_get_section_contents
  3090.              (input_bfd, input_section, (PTR) &ext_gptab,
  3091.               gpentry, sizeof (Elf64_External_gptab))))
  3092.             {
  3093.               free (tab);
  3094.               return false;
  3095.             }
  3096.  
  3097.           bfd_alpha_elf64_swap_gptab_in (input_bfd, &ext_gptab,
  3098.                         &int_gptab);
  3099.           val = int_gptab.gt_entry.gt_g_value;
  3100.           add = int_gptab.gt_entry.gt_bytes - last;
  3101.  
  3102.           exact = false;
  3103.           for (look = 1; look < c; look++)
  3104.             {
  3105.               if (tab[look].gt_entry.gt_g_value >= val)
  3106.             tab[look].gt_entry.gt_bytes += add;
  3107.  
  3108.               if (tab[look].gt_entry.gt_g_value == val)
  3109.             exact = true;
  3110.             }
  3111.  
  3112.           if (! exact)
  3113.             {
  3114.               Elf64_gptab *new_tab;
  3115.               unsigned int max;
  3116.  
  3117.               /* We need a new table entry.  */
  3118.               new_tab = ((Elf64_gptab *)
  3119.                  bfd_realloc ((PTR) tab,
  3120.                           (c + 1) * sizeof (Elf64_gptab)));
  3121.               if (new_tab == NULL)
  3122.             {
  3123.               free (tab);
  3124.               return false;
  3125.             }
  3126.               tab = new_tab;
  3127.               tab[c].gt_entry.gt_g_value = val;
  3128.               tab[c].gt_entry.gt_bytes = add;
  3129.  
  3130.               /* Merge in the size for the next smallest -G
  3131.              value, since that will be implied by this new
  3132.              value.  */
  3133.               max = 0;
  3134.               for (look = 1; look < c; look++)
  3135.             {
  3136.               if (tab[look].gt_entry.gt_g_value < val
  3137.                   && (max == 0
  3138.                   || (tab[look].gt_entry.gt_g_value
  3139.                       > tab[max].gt_entry.gt_g_value)))
  3140.                 max = look;
  3141.             }
  3142.               if (max != 0)
  3143.             tab[c].gt_entry.gt_bytes +=
  3144.               tab[max].gt_entry.gt_bytes;
  3145.  
  3146.               ++c;
  3147.             }
  3148.  
  3149.           last = int_gptab.gt_entry.gt_bytes;
  3150.         }
  3151.  
  3152.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  3153.          elf_link_input_bfd ignores this section.  */
  3154.           input_section->flags &=~ SEC_HAS_CONTENTS;
  3155.         }
  3156.  
  3157.       /* The table must be sorted by -G value.  */
  3158.       if (c > 2)
  3159.         qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
  3160.  
  3161.       /* Swap out the table.  */
  3162.       ext_tab = ((Elf64_External_gptab *)
  3163.              bfd_alloc (abfd, c * sizeof (Elf64_External_gptab)));
  3164.       if (ext_tab == NULL)
  3165.         {
  3166.           free (tab);
  3167.           return false;
  3168.         }
  3169.  
  3170.       for (i = 0; i < c; i++)
  3171.         bfd_alpha_elf64_swap_gptab_out (abfd, tab + i, ext_tab + i);
  3172.       free (tab);
  3173.  
  3174.       o->_raw_size = c * sizeof (Elf64_External_gptab);
  3175.       o->contents = (bfd_byte *) ext_tab;
  3176.  
  3177.       /* Skip this section later on (I don't think this currently
  3178.          matters, but someday it might).  */
  3179.       o->link_order_head = (struct bfd_link_order *) NULL;
  3180.     }
  3181. #endif
  3182.  
  3183.     }
  3184.  
  3185.   /* Invoke the regular ELF backend linker to do all the work.  */
  3186.   if (! bfd_elf64_bfd_final_link (abfd, info))
  3187.     return false;
  3188.  
  3189.   /* Now write out the computed sections.  */
  3190.  
  3191. #ifdef ERIC_neverdef
  3192.   if (reginfo_sec != (asection *) NULL)
  3193.     {
  3194.       Elf64_External_RegInfo ext;
  3195.  
  3196.       bfd_alpha_elf64_swap_reginfo_out (abfd, ®info, &ext);
  3197.       if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
  3198.                       (file_ptr) 0, sizeof ext))
  3199.     return false;
  3200.     }
  3201. #endif
  3202.  
  3203.   if (mdebug_sec != (asection *) NULL)
  3204.     {
  3205.       BFD_ASSERT (abfd->output_has_begun);
  3206.       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
  3207.                            swap, info,
  3208.                            mdebug_sec->filepos))
  3209.     return false;
  3210.  
  3211.       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
  3212.     }
  3213.  
  3214.   if (gptab_data_sec != (asection *) NULL)
  3215.     {
  3216.       if (! bfd_set_section_contents (abfd, gptab_data_sec,
  3217.                       gptab_data_sec->contents,
  3218.                       (file_ptr) 0,
  3219.                       gptab_data_sec->_raw_size))
  3220.     return false;
  3221.     }
  3222.  
  3223.   if (gptab_bss_sec != (asection *) NULL)
  3224.     {
  3225.       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
  3226.                       gptab_bss_sec->contents,
  3227.                       (file_ptr) 0,
  3228.                       gptab_bss_sec->_raw_size))
  3229.     return false;
  3230.     }
  3231.  
  3232.   return true;
  3233. }
  3234.  
  3235. /* ECOFF swapping routines.  These are used when dealing with the
  3236.    .mdebug section, which is in the ECOFF debugging format.  Copied
  3237.    from elf32-mips.c. */
  3238. static const struct ecoff_debug_swap
  3239. elf64_alpha_ecoff_debug_swap =
  3240. {
  3241.   /* Symbol table magic number.  */
  3242.   magicSym2,
  3243.   /* Alignment of debugging information.  E.g., 4.  */
  3244.   8,
  3245.   /* Sizes of external symbolic information.  */
  3246.   sizeof (struct hdr_ext),
  3247.   sizeof (struct dnr_ext),
  3248.   sizeof (struct pdr_ext),
  3249.   sizeof (struct sym_ext),
  3250.   sizeof (struct opt_ext),
  3251.   sizeof (struct fdr_ext),
  3252.   sizeof (struct rfd_ext),
  3253.   sizeof (struct ext_ext),
  3254.   /* Functions to swap in external symbolic data.  */
  3255.   ecoff_swap_hdr_in,
  3256.   ecoff_swap_dnr_in,
  3257.   ecoff_swap_pdr_in,
  3258.   ecoff_swap_sym_in,
  3259.   ecoff_swap_opt_in,
  3260.   ecoff_swap_fdr_in,
  3261.   ecoff_swap_rfd_in,
  3262.   ecoff_swap_ext_in,
  3263.   _bfd_ecoff_swap_tir_in,
  3264.   _bfd_ecoff_swap_rndx_in,
  3265.   /* Functions to swap out external symbolic data.  */
  3266.   ecoff_swap_hdr_out,
  3267.   ecoff_swap_dnr_out,
  3268.   ecoff_swap_pdr_out,
  3269.   ecoff_swap_sym_out,
  3270.   ecoff_swap_opt_out,
  3271.   ecoff_swap_fdr_out,
  3272.   ecoff_swap_rfd_out,
  3273.   ecoff_swap_ext_out,
  3274.   _bfd_ecoff_swap_tir_out,
  3275.   _bfd_ecoff_swap_rndx_out,
  3276.   /* Function to read in symbolic data.  */
  3277.   elf64_alpha_read_ecoff_info
  3278. };
  3279.  
  3280. #define TARGET_LITTLE_SYM    bfd_elf64_alpha_vec
  3281. #define TARGET_LITTLE_NAME    "elf64-alpha"
  3282. #define ELF_ARCH        bfd_arch_alpha
  3283. #define ELF_MACHINE_CODE     EM_ALPHA
  3284. #define ELF_MAXPAGESIZE     0x100000
  3285.  
  3286. #define bfd_elf64_bfd_link_hash_table_create \
  3287.   elf64_alpha_bfd_link_hash_table_create
  3288.  
  3289. #define bfd_elf64_bfd_reloc_type_lookup \
  3290.   elf64_alpha_bfd_reloc_type_lookup
  3291. #define elf_info_to_howto \
  3292.   elf64_alpha_info_to_howto
  3293.  
  3294. #define elf_backend_object_p \
  3295.   elf64_alpha_object_p 
  3296. #define elf_backend_section_from_shdr \
  3297.   elf64_alpha_section_from_shdr
  3298. #define elf_backend_fake_sections \
  3299.   elf64_alpha_fake_sections
  3300. #define elf_backend_additional_program_headers \
  3301.   elf64_alpha_additional_program_headers
  3302.  
  3303. #define bfd_elf64_bfd_is_local_label \
  3304.   elf64_alpha_is_local_label
  3305. #define bfd_elf64_find_nearest_line \
  3306.   elf64_alpha_find_nearest_line
  3307.  
  3308. #define elf_backend_check_relocs \
  3309.   elf64_alpha_check_relocs
  3310. #define elf_backend_create_dynamic_sections \
  3311.   elf64_alpha_create_dynamic_sections
  3312. #define elf_backend_adjust_dynamic_symbol \
  3313.   elf64_alpha_adjust_dynamic_symbol
  3314. #define elf_backend_size_dynamic_sections \
  3315.   elf64_alpha_size_dynamic_sections
  3316. #define elf_backend_relocate_section \
  3317.   elf64_alpha_relocate_section
  3318. #define elf_backend_finish_dynamic_symbol \
  3319.   elf64_alpha_finish_dynamic_symbol
  3320. #define elf_backend_finish_dynamic_sections \
  3321.   elf64_alpha_finish_dynamic_sections
  3322. #define bfd_elf64_bfd_final_link \
  3323.   elf64_alpha_final_link
  3324.  
  3325. #define elf_backend_ecoff_debug_swap \
  3326.   &elf64_alpha_ecoff_debug_swap
  3327.  
  3328. /*
  3329.  * A few constants that determine how the .plt section is set up.
  3330.  */
  3331. #define elf_backend_want_got_plt 0
  3332. #define elf_backend_plt_readonly 0
  3333. #define elf_backend_want_plt_sym 1
  3334.  
  3335. #include "elf64-target.h"
  3336.